my codes occur some problem inside.whn i answered 'N' to the last question(DO YOU WISH TO CONTINUE?(Y/N)), the program will repeat run again, but i cnt enter student name!anyone can help me solve the problem>?

import java.util.Scanner;

public class homewok{
	public static void main(String args[]){

		Scanner xx = new Scanner(System.in);

		boolean vv = true;
		do{

		System.out.println("----------------------------------------------");
		System.out.println("WELCOME TO  BOOKING SYSTEM");
		System.out.println("----------------------------------------------");
		System.out.println("\n---PLEASE ENTER YOUR PERSONAL INFORMATION---");

		System.out.print("\nPLEASE ENTER STUDENT NAME						: ");
		String name = xx.nextLine();

		boolean ag = true;
		do{
		System.out.print("\nPLEASE ENTER STUDENT ID NUMBER(eg : 1xxxxx) 	: TP ");
		int TP = xx.nextInt();

		if (TP < 100000){
			System.out.println("LOGIN FAIL!PLEASE ENTER AGAIN.");
			ag = true;}

		else if (TP> 999999){
			System.out.println("LOGIN FAIL!PLEASE ENTER AGAIN.");
			ag = true;}

		else if ((TP>=100000) && (TP<=999999)){
			System.out.println("LOGIN SUCCESSFUL!\nPLEASE CONTINUE TO FILL IN YOUR PERSONAL INFORMATION.");
			ag = false;}

		}while (ag == true);

		System.out.print("\nPLEASE ENTER YOUR SEX(M/F) : ");
		char s = xx.next().charAt(0);

		if (s=='m')
			System.out.println("WELCOME TO BOOKING SYSTEM, MR. "+(name));

		else if (s=='f')
			System.out.println("WELCOME TO BOOKING SYSTEM, MISS "+(name));

		System.out.print("DO YOU WISH TO CONTINUE?(Y/N) : ");
		char oo = xx.next().charAt(0);

		if (oo == 'y'){
			System.out.println("PLEASE CONTINUE.");
			vv = true;}

		else if (oo == 'n'){
			System.out.println("PLEASE ENTER YOUR PERSONAL INFORMATION AGAIN.");
			vv = false;}

		}while(vv == false);

	}
}

Dani AI

Generated

Short diagnosis: the problem comes from mixing Scanner token methods (next(), nextInt()) with line-oriented input (nextLine()). As noted, a token read (like nextInt() or next()) leaves the line terminator in the buffer so a subsequent nextLine() can return an empty string. was right to suggest using consistent reads. 's final fix (using next() everywhere) works because it avoids nextLine(), but there are safer, clearer patterns.

A robust, recommended approach is to read whole lines and parse them. That avoids Scanner surprises and lets you validate cleanly:

String line;
int tp;
while (true) {
  System.out.print("PLEASE ENTER STUDENT ID NUMBER (eg: 1xxxxx) : TP ");
  line = sc.nextLine().trim();
  try {
    tp = Integer.parseInt(line);
    if (tp >= 100000 && tp <= 999999) break;
    System.out.println("ID must be a 6-digit number.");
  } catch (NumberFormatException e) {
    System.out.println("Please enter digits only.");
  }
}

If keeping token methods is preferred, always consume the leftover end‑of‑line after nextInt() before calling nextLine():

int tp = sc.nextInt();
sc.nextLine(); // eat the '\n' left by nextInt()
String name = sc.nextLine();

Also fix the loop and naming: give the loop a clear boolean (continueEntering), set it to true to repeat, and use equalsIgnoreCase() on trimmed input for Y/N checks. Avoid using next().charAt(0) without checking for empty input; instead read a line, trim(), and test startsWith("m") / equalsIgnoreCase("m"). Finally, always validate/parset input inside try/catch so malformed input does not break the program.

Recommended Answers

All 12 Replies

use .equals() when comparing strings rather than ==
try to read this link for more info

use .equals() when comparing strings rather than ==
try to read this link for more info

sry~i not really understand!i tried ad~if i change "if (s=='m')" to "if (s.equals("m"))"....it dosnt run...can u further explain to me?so sry for making trouble to u...><

change oo and s to string and use nextLine() for receiving input
e.g

String oo = xx.nextLine();

also just noticed now that the program continues when vv is equal to false and when you choose n the program continues to run cause it remains to be false

use .equals() when comparing strings rather than ==
try to read this link for more info

Good advice, yes, but in this case the code is comparing chars, not Strings

char oo = xx.next().charAt(0);
if (oo == 'y')...{

so .equals cannot be used in this situation.


Maybe the problem here is that the y/n is read with a next(), which will leave a newline character unprocessed in the scanner's buffer, so the student name's nextLine() returns the remainder of the current line, ie "".
At line 49 try a nextLine() rather then a next() to read the y/n so that the newline character is processed normally.

Good advice, yes, but in this case the code is comparing chars, not Strings

char oo = xx.next().charAt(0);
if (oo == 'y')...{

so .equals cannot be used in this situation.


Maybe the problem here is that the y/n is read with a next(), which will leave a newline character unprocessed in the scanner's buffer, so the student name's nextLine() returns the remainder of the current line, ie "".
At line 49 try a nextLine() rather then a next() to read the y/n so that the newline character is processed normally.

Yes... sorry about that just noticed that char was used instead of string so the next step would have been my previous post... :$

It's correct but unnecessary... my bad

><.....ya~i changed ad!but the same problm still here....erm....if i change line 49 to "Line()", the program dosent ask user to enter Y/N....just display the question.

import java.util.Scanner;

public class homewok{
	public static void main(String args[]){

		Scanner xx = new Scanner(System.in);

		boolean vv = false;
		do{

		System.out.println("----------------------------------------------");
		System.out.println("WELCOME TO  BOOKING SYSTEM");
		System.out.println("----------------------------------------------");
		System.out.println("\n---PLEASE ENTER YOUR PERSONAL INFORMATION---");

		System.out.print("\nPLEASE ENTER STUDENT NAME						: ");
		String name = xx.nextLine();

		boolean ag = true;
		do{
		System.out.print("\nPLEASE ENTER STUDENT ID NUMBER(eg : 1xxxxx) 	: TP ");
		int TP = xx.nextInt();

		if (TP < 100000){
			System.out.println("LOGIN FAIL!PLEASE ENTER AGAIN.");
			ag = true;}

		else if (TP> 999999){
			System.out.println("LOGIN FAIL!PLEASE ENTER AGAIN.");
			ag = true;}

		else if ((TP>=100000) && (TP<=999999)){
			System.out.println("LOGIN SUCCESSFUL!\nPLEASE CONTINUE TO FILL IN YOUR PERSONAL INFORMATION.");
			ag = false;}

		}while (ag == true);

		System.out.print("PLEASE ENTER YOUR SEX(M/F) : ");
		String s = xx.next();

		if (s.equals("m"))
			System.out.println("WELCOME TO BOOKING SYSTEM, MR. "+(name));

		else if (s.equals("f"))
			System.out.println("WELCOME TO BOOKING SYSTEM, MISS "+(name));

		System.out.print("\nDO YOU WISH TO CONTINUE?(Y/N) : ");
		String oo = xx.next();

		if (oo.equals("y")){
			System.out.println("PLEASE CONTINUE.");
			vv = true;}

		else if (oo.equals("n")){
			System.out.println("PLEASE ENTER YOUR PERSONAL INFORMATION AGAIN.");
			vv = false;}

		}while(vv == false);

	}
}

That's nextLine(), not Line().

Line 49 works fine without the ...Line() if you're using Strings

That's nextLine(), not Line().

Would he really just use Line() :) ;)

yaya~if i put "...Line()",the program can ask user to enter student name ad!but the program dosent ask user to enter Y/N(line 47) .....><....why?

would you feel better if you don't use them...
like just use next() in lines 17, 39, 48

commented: thx so much~problem sloved ad! +0
WELCOME TO FORUMS, puppycrazy
WOULD YOU LIKE TO ANNOY YOUR USERS?(Y/N) : y
IN THAT CASE PLEASE CONTINUE USING ALL CAPS

WOULD YOU LIKE TO GET GOOD ANSWERS?(Y/N) : y
IN THAT CASE PLEASE CONSIDER PROOFREADING YOUR POSTS TO ENSURE THEY MAKE SENSE. ALSO,PLEASE!AVOID>USING~WEIRD.......><....PUNCTUATION, SPACES EXIST FOR A REASON

thx so much to u all!sry for wasting you all so much time!my problem solved ad!by using 'next()' instead of 'nextLine()' in lines 17, 39, 48!!!thx thx thx^^

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.