Hi, ^^
I'll be competing for a java programming contest next week
and one of the instructions is to
"Receive input from the console window (of JCreator)."
Thus, programs shouldn't involve any GUI.
I've found this very useful tool called Scanner in the java.util package.
My self-training was going smoothly
but then i encountered a problem
with a very simple code below.
It's supposed to let the user enter any strings 3 times, and then output all of them afterward.
But, when i typed my second string and hit enter,
the program just suddenly went into the System.out.print() part
and so i could not input my 3rd string.
my 2nd string was stored in the variable c,
while variable b was left empty.
Here's the code:
import java.util.Scanner;
public class Scratch {
public static void main(String[] args) {
Scanner en = new Scanner(System.in);
String a = en.nextLine();
String b = en.nextLine();
String c = en.nextLine();
System.out.print("a: " + a + "\nb: " + b +"\nc: " + c);
}
}
Things go very well if i output some instructions. But it shouldn't really affect anything... am i right?
import java.util.Scanner;
public class Scratch2 {
public static void main(String[] args) {
Scanner en = new Scanner(System.in);
System.out.print("Enter a: ");
String a = en.nextLine();
System.out.print("Enter b: ");
String b = en.nextLine();
System.out.print("Enter c: ");
String c = en.nextLine();
System.out.print("a: " + a + "\nb: " + b +"\nc: " + c);
}
}
Please give my codes a run. And tell me what's wrong... >.<