-
September 17th, 2010, 09:33 AM
#1
Java input
Hi i have a problem with the following code:
<code>
import java.util.Scanner;
public class Woorden{
public static void main (String[] argv){
Scanner input = new Scanner(System.in);
String zin;
System.out.print("Zin ");
if (input.hasNextLine()){
while (input.hasNext()){
System.out.println(input.next());
}
}
}
}
</code>
The program should promt the user for one input line and then print the elements of that line one at a time. However, the program does not stop. The input is entered by using keyboard + command line.
Can anyone help me with this problem?
-
September 17th, 2010, 12:44 PM
#2
Re: Java input
However, the program does not stop.
You need to add code that looks at the input and sees if the user has requested the program to stop.
Otherwise it will continue to wait for the user to enter more data.
Read the API doc for the Scanner class and its methods. Some of the methods "block" meaning the program waits at that instruction until there is input to process.
Norm
-
September 26th, 2010, 03:19 AM
#3
Re: Java input
Originally Posted by NielsNIO
Hi i have a problem with the following code:
<code>
import java.util.Scanner;
public class Woorden{
public static void main (String[] argv){
Scanner input = new Scanner(System.in);
String zin;
System.out.print("Zin ");
if (input.hasNextLine()){
while (input.hasNext()){
System.out.println(input.next());
}
}
}
}
</code>
The program should promt the user for one input line and then print the elements of that line one at a time. However, the program does not stop. The input is entered by using keyboard + command line.
Can anyone help me with this problem?
Can you give us an output that is supposed to be look like?
do you want it to display like this?
Ex.
Zin <string value>
Zin <string value>
Zin <string value>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|