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);

	}
}

Recommended Answers

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

Jump to Post

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

Jump to Post

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

Jump to Post

All 12 Replies

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

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

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
Java: ==,.equals()

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 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.