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... >.<

Recommended Answers

All 11 Replies

Okay, I'll bite.
Here's one:

/Users/jon/java/junk:509 $ java Scratch
qwer
asdf
xcvb
a: qwer
b: asdf
c: xcvb

Here's the other.

[jon: Sat Nov 20 02:23] 
/Users/jon/java/junk:508 $ java Scratch2
Enter a: asdf
Enter b: wer
Enter c: dfgh
a: asdf
b: wer
c: dfgh

Nothing very surprising here. Did you maybe have a seizure on the return key the second time?

T_T
i seriously don't know what's happening T____T

i was inputting in JCreator's console window (GENERAL OUTPUT) though..
hmm.. let me try in cmd

well......
cmd went perfectly ok.
i just installed JCreator Pro, and tried it there.
it went OK.
but still not in my JCreator LE 4.50
i've been using it all my java life T___T.

i even tried to use BufferedReader in java.io package, and still got the same errors.
so i guess the prob was in my JC LE.
>.<

Might be that. I've never heard of JCreator - I tend to work in vi. I use Eclipse on my work machine, and I've never had a problem like that there. If you have to have an IDE, Eclipse seems pretty popular.

i just switched back to JCreator (and Java for that matter) very veryy recently because it's part of the rules in this competition i'll be joining.
hmmm, and it seems that i have gained interest in java once again!
my prof was suggesting Eclipse for our subject in mobile computing which focuses on Google's Android. i only tried Netbeans IDE before. but i guess i'll expose myself to Eclipse pretty soon. :)

*** NEW PROB ***

import java.util.Scanner;
public class Scratch {
    public static void main(String[] args) {
    	Scanner en = new Scanner(System.in);
    	
    	int a = en.nextInt();
    	String b = en.nextLine();
    	String c = en.nextLine();
    	String d = en.nextLine();
    	System.out.print("a: " + a + "\nb: " + b +"\nc: " + c+"\nd: " + d);
    }
}

i want to have an integer input (a) first, then some strings (b,c,d) which can possibly contain spaces.
the original problem above RE-CREATED itself!!!
the 2nd input which is a string for variable b was totally skipped.
it was stored in c, and left b blank!!!

well, i did some research and found out that nextInt() 'accepts' a newline ("\n") after you hit enter for the integer input. it just leaves this "\n" in the input stream which is what the nextLine() for variable b immediately detected (making it appear to have skipped).
this can be ok if i use the next() method instead of nextLine() but spaces are expected in the string inputs.

so a sol'n i had was:

int a = Integer.parseInt(in.nextLine());
    	String b = in.nextLine();
    	String c = in.nextLine();
    	String d = in.nextLine();
    	System.out.print("a: " + a + "\nb: " + b +"\nc: " + c+"\nd: " + d);

...to use nextLine() even for integer inputs! :)
i thought it would work ok too if i use in.next() inside the parseInt()
but nahhhhhh, T_T

is this a glitch?
and is there a way to avoid it other than my parseInt trick????
i really think this has nothing to do with JCreator LE this time
>..<

i thought it would work ok too if i use in.next() inside the parseInt()

next() leaves the newline character in the stream which is consumed by the nextLine() call. Use nextLine() call throughout your code and you should be good to go.

G:\java>javac Scratch2.java

G:\java>java Scratch2
Enter a: 1
Enter b: 2
Enter c: 1
a: 1
b: 2
c: 1
G:\java>

It's Run successfully. there is nothing worng in this code. I Think the proble should be your JDK verson or windows. Re-install your windows and JDK software again then inform us. Thank's

G:\java>javac Scratch2.java

G:\java>java Scratch2
Enter a: 1
Enter b: 2
Enter c: 1
a: 1
b: 2
c: 1
G:\java>

It's Run successfully. there is nothing worng in this code. I Think the proble should be your JDK verson or windows. Re-install your windows and JDK software again then inform us. Thank's

Yes sir! ^__^
I tried it in the command prompt and it worked properly so the problem was not in JDK nor in windows.
I installed JCreator Pro, tried the code there, and it worked properly too.
So the problem was with my outdated JCreator LE 4.50.

If u cud read the replies, there's a new scenario i've posted which seem to have replicated the same problem i encountered originally.
this time, it's using nextInt() before nextLine().

which could be a better approach to avoid such prob?
1. Using nextLine() all throughout and then wrapping some inside parseInt()?
OR
2. Call nextLine() (without assigning it to a variable, yes it's possible!) after every nextInt()?

int a = in.nextInt();
   in.nextLine();
   String b = in.nextLine();

anyways, both approaches get the job done. :)

Either of your approaches is reasonable. parseInt(scan.nextLine()) offers you a potential problem: if the user enters something like "444 sdfadfa", it throws an exception, but the nextInt();nextLine(); solution should be safe there.

If you want to be cautious, you can wrap the parseInt() or the nextInt() in a try block, just in case someone tries to give you a non-int value. That starts to add up to a few lines of code for each int read, so maybe you should make a little method for that, something that would get the nextInt() and then call a nextLine() and, oh, yeah, handle any InputMismatchException from the nextInt() by looping back around to get a good integer from the user.

:) :) :)
thanks!!!
i think i'll settle myself with nextInt();nextLine();
:) :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.