The code doesn't compile. Post the correct code
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
The code is correct. You think it's wrong because the code waits for you to enter more values and while it does that it displays nothing. You should always have print messaged before reading from the console:
do{
<strong>System.out.println("Enter you Name: ");</strong>
String Name=in.nextLine();
......
<strong>System.out.println("Enter 0 or 1 to continue: ");</strong>
n=in.nextInt();
} while (n<2);
<strong>System.out.println("Finished!");</strong>
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
I believe it's because you didn't enter a name the second. Your code is very poor for handling error inputs. Try this:
do{
System.out.println("Enter you Name: ");
String Name=in.nextLine();
System.out.println("You have entered: "+Name);
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
In my PC the code run OK. Try run it again and post what messages you get
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Enter you Name:
You have:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.StringBuffer.charAt(StringBuffer.java:162)
at beffer.Main.main(Main.java:33)
Java Result: 1
Of course the program exited because you didn't enter anything the second time:Enter you Name:
aa bb cc
You have: aa bb cc
Capitalize name: Aa Bb Cc
Middle name is: vBb
First name: Aa
Last name: Cc
Number of Words :3
Enter 0 or 1 to continue:
0
Enter you Name:
dd ee ff
You have: dd ee ff
Capitalize name: Dd Ee Ff
Middle name is: vEe
First name: Dd
Last name: Ff
Number of Words :5
Enter 0 or 1 to continue:
1
Enter you Name:
aa bb
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.StringBuffer.substring(StringBuffer.java:801)
at stam.Main.main(Main.java:47)
You have: aa bb
The third time the program exited with exception because I entered 2 words instead if 3. In your case you entered nothing. You just pressed enter
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Actually the whole thing was my mistake.
I am sorry.
I was using a wrong jdk and version of your code for testing and I am getting the same errors you get.
I tried some things to fix it with no luck.
Finally I came with this idea:
Replace the nextInt with this:
n=Integer.parseInt(in.nextLine());
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Well I don't believe there has something to do with your machine. The problem was the code (and I didn't see it. sorry.)
java.util.Scanner
I believe the problem was the use of both nextLine and nextInt. After you called the nextInt, the loop repeated itself and tried to call the nextLine.
But for some reason that I can't explain it skipped the line you entered or it read on its own an empty line:
nextLine
By adding in both places the nextLine the problem was fixed.
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
oh Thanks.But any way,i have Some problems with my code.Here, how to modify to input array of string and compare the name(compare first name,if the same first name then compare last name).Tell the smallest one.
Actually you cannot input an array of Strings.
Ask the user to enter the length of the array and then do this:
System.out.println("Enter length: ");
int length = Integer.parseInt(in.nextLine());
String [] array = new String[length];
for (int i=0;i<length;i++) {
System.out.println("Enter name:");
array[i] = in.nextLine();
}
Now you have the array and do whatever comparisons you want. Use the compareTo method to compare which String is greater than the other for your sorting. Have a separate method that does that.
If you want to "split" the input try this method:
String input = "Name MN Last";
String [] tokens = input.split(" ");
for (int i=0;i<tokens.length;i++) {
System.out.println(tokens[i]);
}
/////////////////////////////////////////////////////////////
For better solution try:
class Names {
public String first = "";
public String last = "";
public String middle = "";
public Names() {
}
public String toString() {
return first+" "+middle+" "+last;
}
}
String input = "Name MN Last";
String [] tokens = input.split(" ");
for (int i=0;i<tokens.length;i++) {
System.out.println(tokens[i]);
}
Names nm = new Names();
nm.first = tokens[0];
nm.middle = tokens[1];
nm.last = tokens[2];
System.out.println(nm); // the toString method of the object is called automatically
Names [] arrayWithNames = new Names[2];
arrayWithNames[0].first = ....;
arrayWithNames[0].middle = ....;
arrayWithNames[0].last = ....;
for (int i=0;i<arrayWithNames.length;i++) {
System.out.println(arrayWithNames[i]);
// arrayWithNames is an array
// arrayWithNames[i] is an Names object
}
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Oh thanks.But what if we dont know the length before.As long as we input name,the program with compare and sort them.
If you don't know the length use the Vector class.
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
You can't put Strings into an integer array. Create a String array instead.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354