Why Print Char 0 (zero) plus String Does Not Work

Apr 3, 2015

I have a simple doubt. I was studying and create some code to check the result and I found out a strange situation.
 
Whats wrong with this code? Why it does not print anything?
 
char = 0; //integer value
System.out.println( c +" String ");
 
and why this next works very well?
 
char = 1; //integer value
System.out.println( c +" String ");
 
I know that char is stored as a positive integer and assign with 0 is different of assign with '0'.

View Replies


ADVERTISEMENT

User Input 20 Char And Program Will Print Most Common - Exception In Thread Main

Dec 17, 2014

My program is user input 20 char and the program will print the most common. So I use another int arr which count the number appears in the original array. i know its not so Effective but I don't know why it run but it stop in the middle. I got this code :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
at Ex2.common(Ex2.java:39)
at Ex2.main(Ex2.java:23)

import java.util.Scanner;
class Ex2 {
public static void main(String[] arg) {
Scanner reader = new Scanner (System.in);
char[] arr=new char[20];
System.out.println("Please enter 20 chars:");
for (int i=0;i<20;i++)

[Code] ....

View Replies View Related

Char To String

Oct 14, 2014

I need making char[] to a string im not entirely sure what to change i'm just suppose to use a string value but the upperclassmen used char:

char[] number = clear.toLowerCase().toCharArray();
for(int c = 0; c < number.length; c++) {
if(digit[c] < 'a' || digit[c] > 'z')
continue;

[code]...

View Replies View Related

How To Put All Char In Loop To String

Mar 16, 2014

I can't figure out how to have all of the random characters generated to go into the String. Below I can only get the last character to covert over to a String.

System.out.println("Original random character string:");
String printingString = "a";
for (int i = 0; i < 50; i++)//loop to obtain 50 random characters
{
char randomChar = (char) ((Math.random() * 255) +32);
System.out.print(randomChar);
printingString = Character.toString(randomChar); }
return printingString; }

View Replies View Related

Getting Char At A Location In String

Oct 21, 2014

I found a fun program online and something so simple is giving me an issue. I c++ it is pretty simple fix, I can just call the strings location like an array. In java this is not the case. So far i have tried:

myString.charAt();
myString.indexOf();

There are a few other I found on google but I forget at the moment. I am just trying to close the gap on a string. It was a full sentence and I used replaceAll a few times to get several words I didn't want in the file out.

View Replies View Related

Comparing Char In If That Convert To String

Apr 30, 2014

what will i compare in if statemet is the 1st letter of each if i have code="a" and name="Angelina" first letter of each is "a" and "A" then in convert it to string so that i can make it uppercase but when i compare it in if statement it always go into "not x" but the ouput that im getting is x=A y=A then it always direct me into else statement.

String code = "a";
String name = "Angelina";
char c = code.charAt(0);
char n = name.charAt(0);

[code]...

View Replies View Related

Check How Many Times Char Is Used In The String

Apr 1, 2014

I tried to make a program that takes a string str, and char a and checks how many times the char is used in the string.

Example: the string Welcome and the letter e, is 2 times. so the program should print 2.

It compiles but when I run it and enter the information, i cannot get the printing line out.

Heres my code:

import java.util.Scanner;
class program
{
public static void main(String[] args) {
Scanner user_input=new Scanner(System.in);
String str;
String b;
System.out.print("Please enter a word");

[Code] .....

View Replies View Related

Unable To Count Each Char In A String

Jun 5, 2014

I am trying to count each char in a string. For example A = 1, B =2, C=3, I am not looking for their binary value. So the word "At" would

AT= (A=1 +T=20)=21.

I know how to do this in C++ because I am able to treat a string like an array.

Java Code: void printFile()
{
int sum=0;
String line;
for(char cr ='A';cr<'Z';cr++)
{
for(int i=0; i<myList.size();i++)

[Code]...

View Replies View Related

If Statement - Char To String Casting?

Aug 3, 2014

import java.io.IOException;
import java.util.*;
public class Guesser {
public static void main(String[] args) throws IOException {
 char[] alphabet = "abcdefghijklmnopqrstuvwxyz1234567890 .,:;'-".toCharArray();

[Code] .....

I'm writing a program which will take a three letter word (for now) and then try to guess the word over and over again until it finds it, then print the word and the amount of tries it took to find it.

The problem: at the moment the program will find the word but not break out of the for loop when it does. I think it doesn't like the char to String conversion somewhere along the line.

View Replies View Related

Split String From Space Char

Mar 30, 2014

I want to cut my string from space char but i am getting exception....

Java Code:

import java.util.Scanner;
import java.util.StringTokenizer;
public class NameSurname {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String s0,s1=null,s2 = null,s3=null;
s0=sc.next();

[Code] ....

Console:
Lionel andres messi
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at java.util.StringTokenizer.nextToken(Unknown Source)
at com.parikshak.NameSurname.main(NameSurname.java:15) mh_sh_highlight_all('java');
I/p -O/p:

my s0=Lionel andres Messi

And I want to break it as soon as i find space and save it in s1,s2 and s3

s1=Lionel
s2=andres
s3=messi

View Replies View Related

Check How Many Times Char Is Used In String

Apr 1, 2014

I tried to make a program that takes a string str, and char a and checks how many times the char is used in the string. Example: the string Welcome and the letter e, is 2 times. so the program should print 2. It compiles but when I run it and enter the information, i cannot get the printing line out.

Heres my code:

import java.util.Scanner;
class program
{
public static void main(String[] args) {
Scanner user_input=new Scanner(System.in);
String str;
String b;
System.out.print("Please enter a word");
str=user_input.next();

[Code] ....

View Replies View Related

Finding A String Word In Char Array

Feb 13, 2015

I'm trying to find a word in an array of char.....but I'm stuck. How to formulate the code to step through the array and pick out the word. This is what I have so far...

public static void searchAcross(String string, char[][] puzzle) {
// Gets the number of rows in the matrix
int rowLength = puzzle.length;
//Gets the number of columns in the matrix.
int colLength = puzzle[0].length;

[Code] ....

View Replies View Related

How To Compare String Input To Char Array

Feb 8, 2014

What I'm trying to do is compare String input to a char array. Let me make it a little more plain, I'm working on a cipher assignment, and my line of thought is this: I will get String input from the user, COMPARE the characters in the string input to an alphabet array, which will then be compared to the cipher array so that the cipher's counterpart can be chosen over the alphabet's. Any way that I might compare the random input keyed in by the user to that alphabet array?

View Replies View Related

Convert A String Of ASCII Code To Char?

Aug 25, 2014

I am trying to figure out how to convert a string of ASCII code into char.I know that you can use (char) to convert it, but the issue is you cannot really just it for Strings.

View Replies View Related

Return Given Char That Matches Input String

Apr 17, 2015

Processing a string. How would I only return a given char that matches the input string e.g. v and/or n and/or m.

Everything else that does not match will return a '*' - e.g. user input = t result = *

I assume I need to also iterate through this input string using charAt() ?

View Replies View Related

Convert String To Two Dimensional Char And Output Is Equal To Matrix

Jan 6, 2014

I have written the code:

Java Code:

public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
System.out.print("Type your text: ");
String text = input.nextLine();
int counter = text.length();
if(text.length()> 16)

[Code] ....

And input is: abcdefghijklm

output is:

Java Code:

a b c d
e f g h
i j k l
m x x x mh_sh_highlight_all('java');

So all i want is, if i type: abcdefghijklm

I want this output:

Java Code:

a e i m
b f j x
c g k x
d h l x mh_sh_highlight_all('java');

View Replies View Related

String Does Not Work As Passcode

Nov 4, 2014

I have being trying to figure out why my string doesn't work as a passcode and I can't seem to figure out why.

import java.util.*;
public class Login
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Hello there, please state your name");
String userName = in.nextLine();

[Code] ....

View Replies View Related

Take Replacement String Entered By User And Print Out New String

May 22, 2014

I'm having trouble with the last few lines of the code. It's supposed to take a replacement string entered by the user and print out the new string. For some reason it's now allowing me to enter a replacement string

import java.util.Scanner;
public class Project02 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a long string: ");
String lString = keyboard.nextLine();

[Code] ....

Output:

Enter a long string: the quick brown fox jumped over the lazy dog
Enter a substring: jumped
Length of your string: 44
Length of your substring: 6
Starting position of your substring in string: 20
String before your substring: the quick brown fox
String after your substring: over the lazy dog
Enter a position between 0 and 43: 18
The character at position 18 is x

Enter a replacement string: Your new string is: the quick brown fox over the lazy dog <------ isn't taking user input

View Replies View Related

Simple If Statement Taking A String Input Doesn't Work

Jun 6, 2014

Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
if (input == "next") {
System.out.print("good Job!");
}
else {
System.out.println("Why ?");
}

//If I type in next it prints out "Why ?" instead of "Good Job!" - Why though ?

View Replies View Related

Accept String And Number Of Repetitions As Parameters And Print String Given Number Of Times

Oct 3, 2014

I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...

This is the question:

Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:

printStrings("abc", 5);

will print the following output: abcabcabcabcabc

This is what I attempted:

public class printStringsproject {
public static void printStrings(int abc, int number) {
for (int i = 1; i <= number; i++) {
System.out.print("abc");
}
}
public static void main(String[] args) {
printStrings(1, 5);
}
}

View Replies View Related

Convert String Array To Char Array

Apr 12, 2015

I have an array of Strings, one on each line and I need to convert them into an array of char's.

For Example:

This
is
an
Example
of
what
my
input
is.

In order to accomplish that I did the following-

String[] lotsOfText = a.gettingAnArrayAsAReturn();
char [][] myCharArray = new char [lotsOfText.length] [lotsOfText.length];
for(int i=0; i<lotsOfText.length; i++){
for(int j=0;j<lotsOfText[i].length();j++){
myCharArray[i][j] = lotsOfText[j].charAt(j); }}

But whenever I try this and then try to print the output :

for (int i = 0; i < lotsOfText.length; i++) {
for (int j = 0; j < lotsOfText[i].length(); j++) {
System.out.print(myCharArray[i][j]);
}
}

I get nothing. I'm not sure what's the flaw in my logic, is it the char array initialization that's wrong or is it something else ?

View Replies View Related

How To Print String Only Five Times

Feb 11, 2015

I can't figure out how to print this string only five times. I tried to use the * operator, but that doesn't work with strings apparently, unles i'm not importing correctly.
 
import java.lang.String;
public class Looparray
{
public static void main(String args[] {
for (String myStr= "Hello there!";;) {
System.out.print (myStr);
System.out.print("
");
}
}
}

View Replies View Related

How To Print 5th Letter Of A String

Nov 11, 2014

So if I wanted to tell the user that the fifth letter they entered is "____" how would I do that.

I am prompting the user to enter a word and then displaying the length of the String. Now I want to display to the user what the fifth letter of the String they entered is.

import java.util.Scanner;
public class StringPractice
{
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String word;
int lenght;

[Code] ....

View Replies View Related

Char Array To A String Array With Hex

Nov 16, 2010

I have a string array but each cell in the 1d string array stores each character the text file is :

"START START START
The quick brown fox jumps over the lazy dog 1234567890-= !"$%^&*()_+ QWERTYUIOP{}ASDFGHJKL:@~|ZXCVBNM<><? /.,mnbvcxzasdfghjkkl;'#][poiuytrewq789654123.0
+-*/``""$% hello this is a test file using all the characters availible on the keyboard for input END END END END"

so in the string it is:[0] = S, [1]=A, [2]=R ...ect along the text basically i need to convert each character in each cell of the 1d string array to its hesidecimal value..i have created my own method which will take in a char and return a string containing the charcters hex value.

public static String toHex(char c) {
char char2ascii = c;
int i = 0;
int num = (int) char2ascii;
String hex ="";

[code]...

what i want to do is run each cell through the toHex method so i eventually have a string array containing the hex value of each character in my text.

example..i want:

String[] hexarray = S, T, A, R, T

a run it through my method to convert to hex then it will become

String[] hexarray = 53, 54, 41, 52, 54

Im not allowed to use inbuilt libarys and classes to do the hex conversion thats why i have my own method for it .

View Replies View Related

Print Pyramid String Error

Nov 18, 2014

I'm having a small error with my code, it is getting rid of the first letter of the string after the first line printed.

import java.util.*;
public class PrintPyramidName {
public static void main(String[] args) {
System.out.println("What is your name?");
Scanner input = new Scanner(System.in);

[Code] ....

View Replies View Related

Print String From Starting Character Given By User

Jul 14, 2014

Now I am doing dictionary app using files..

Here i want to print the String from Starting character which is given by user..Here i given below the image like my concept..here we put .(dot and enter R)eclipse will print the method name which is starting from R..

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved