ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
import java.util.Scanner;
class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 0;
int i=0;
System.out.println("How many names?");
n = sc.nextInt();
String[] arrayString = new String[n];
arrayString[i] = sc.nextLine(); // notice this request for input before the loop (seems to be useless)
for (i=0; i<n; i++) {
System.out.println("Give the name:");
arrayString[i] = sc.nextLine(); // request for input inside the loop.
}
for (i=0; i<arrayString.length; i++) {
System.out.println(arrayString[i]);
}
}
}
Ok the above code is working fine and it got me 30mins to find the right algorithm.
The strange thing is that: Let's rewrite the above code slightly different (see comments):
Code:
import java.util.Scanner;
class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 0;
int i=0;
System.out.println("How many names?");
n = sc.nextInt();
String[] arrayString = new String[n];
// ***Remove the request before the loop.
for (i=0; i<n; i++) {
System.out.println("Give the name:");
arrayString[i] = sc.nextLine(); // ***request for input only inside the loop.
}
for (i=0; i<arrayString.length; i++) {
System.out.println(arrayString[i]);
}
}
}
The second program seems more correct. But in fact if you run it it will ask you how many names? Let's say 3...
The result will be something like:
Give the name:
Give the name:
me
Give the name:
and you
me
and you
Notice that the array at the point [0] has been set to An empty string by default. The first program works fine for n=3 again:
Give the name:
Me
Give the name:
and you
Give the name:
3rd name
Me
and you
3rd name
Can anybody give me a comprehensive explanation why that happens? Because believe me.. I might solved out my problem but I still don't realize why I have to put a duplication of input over the loop.
I didn't check with the API so this is just a guess
I guess that sc.nextInt() only read the next integer, without bothering if there is any character, including newline(\n), following that number.
As a concrete example, say you enter "3" when you are asked to specify the number of names. This input reaches the program as "3\n". When you call sc.nextInt(), it only takes the "3" and leave the "\n" in the scanner's buffer. This "\n" will be read the next time you call sc.nextLine(), which leads to the problem in the second code.
Hm i don't know if you're right but your answer seems to be logic... Is there any way to change this? Because is a little annoying to put a useless scanner before the loop.. You know is a bad practice! But thanks for your answer anw!
yeah koyi is right but I am asking if is there any way to get the sc.nextInt(); to handle the "/n" If i do this the scanner over the loop will be non-needed. I have no problem with the program as it works fine, but is a little annoying the scanner over the loop :P
PS:And the solution is not to get the n=3 as a string and convert it to int... This would be even annoying
As koyi said, and as you now realize, the problem is that java.util.Scanner's "nextInt()" function just grabs the characters it needs (for example, "3"), leaving the rest of the line to be parsed by subsequent calls ... including the "\n", which delimits the end of the line.
One solution (since you expect a line)... is to simply ask for a line. In other words, use "nextLine()", and then convert the input:
Code:
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print ("Enter #/names: ");
String s = sc.nextLine ();
int n = Integer.parseInt (s);
System.out.println ("OK: please enter " + n + " names:");
String[] arrayString = new String[n];
for (int i=0; i < n; i++) {
System.out.print ("name #" + (i+1) + ": ");
arrayString[i] = sc.nextLine();
}
for (int i=0; i<arrayString.length; i++) {
System.out.println("name[" + i + "]: " + arrayString[i]);
}
}
}
PS:And the solution is not to get the n=3 as a string and convert it to int... This would be even annoying
This would be a nice solution but it still annoying. Look I am not trying to bother you and thanks for your reply but what I am looking for is if is there any way to make the nextInt(); handle the "\n"... If isn't there any solution just tell it to me... Thanks for your support all of you!
Strange behaviour? So you'd actually prefer your methods to do more than they should?
Anyway, there is an alternative solution called a StreamTokenizer. It's getting to be legacy but it still comes in handy now and again. In case you wonder, a StreamTokenizer takes input and parses it by taking spaces as delimiters. It can distinguish between numbers, words and Strings but it's up to you to provide the implementation. As you can guess, it involves more coding than Scanner methods.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
Advertisement
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Click Here to receive a complimentary subscription courtesy of LQ.