Hello everyone,

I am writing a simple program for homework that processes the users income taxes. Part of the assignment is to allow the user to process a second user if they desire.

My problem is that when the user elects to go ahead with round number two. The program skips the part of allowing the user to enter their name and goes right to the next output line. I believe in c++ something like cin.ignore would do the trick, but I have no idea what to do in Java. I cannot figure out how to use the reset command nor the clear function..

I have tried placing the variables in and out of the loop but nothing is working .. any help is GREATLY appreciated

Here is the output,,, code is kind of long to post on here ,, if anyone is willing to look at it Im more than happy to post ..

Please enter your first and last name: sdiksdi
Hello sdiksdi please enter the following information
and I will compute your Federal Income Tax.

Enter your salary income: 232

Enter your interest income: 2323

Enter the total number of exemptions withheld: 3

Enter the total amount of tax withheld: 233
Your refund amount is $1226.0
sdiksdi if you would like to process another tax payer please enter 1,
otherwise enter 0 to end this program:1
Please enter your first and last name: Hello please enter the following information
and I will compute your Federal Income Tax.

Enter your salary income:

Dani AI

Generated

This is the classic Scanner newline problem. The loop is seeing an empty name because a previous numeric read left the line terminator in the input buffer. was on the right track with nextLine(), but a clearer, more robust approach is to either always read whole lines and parse them, or explicitly consume the leftover newline when you mix token-based reads and line-based reads.

Why it happens: nextInt() / nextDouble() read the number token but not the trailing newline. A subsequent nextLine() then returns that leftover empty string. Scanner.reset() does not clear the input buffer — it only resets scanner state such as delimiters/locale — so it will not fix this.

Two practical fixes:

  1. Read lines only and parse numbers (recommended for simple console apps). This avoids mixing token/line APIs.
Scanner sc = new Scanner(System.in);
boolean again;
do {
  System.out.print("Please enter your first and last name: ");
  String name = sc.nextLine();

  System.out.print("Enter your salary income: ");
  double salary = Double.parseDouble(sc.nextLine());

  // parse other fields similarly, compute refund...

  System.out.print("Process another? (1=yes, 0=no): ");
  again = "1".equals(sc.nextLine().trim());
} while (again);
  1. If you prefer nextInt()/nextDouble(), consume the trailing newline before any nextLine():
double salary = sc.nextDouble();
sc.nextLine(); // consume the newline before reading the name with nextLine()

Extra tips: keep a single Scanner on System.in (don’t create/close multiple) and validate/trim user input (catch NumberFormatException if parsing). That will stop the skip and make the loop reliable for multiple taxpayers.

Recommended Answers

All 2 Replies

Hello everyone,

I am writing a simple program for homework that processes the users income taxes. Part of the assignment is to allow the user to process a second user if they desire.

My problem is that when the user elects to go ahead with round number two. The program skips the part of allowing the user to enter their name and goes right to the next output line. I believe in c++ something like cin.ignore would do the trick, but I have no idea what to do in Java. I cannot figure out how to use the reset command nor the clear function..

I have tried placing the variables in and out of the loop but nothing is working .. any help is GREATLY appreciated

Here is the output,,, code is kind of long to post on here ,, if anyone is willing to look at it Im more than happy to post ..

Please enter your first and last name: sdiksdi
Hello sdiksdi please enter the following information
and I will compute your Federal Income Tax.

Enter your salary income: 232

Enter your interest income: 2323

Enter the total number of exemptions withheld: 3

Enter the total amount of tax withheld: 233
Your refund amount is $1226.0
sdiksdi if you would like to process another tax payer please enter 1,
otherwise enter 0 to end this program:1
Please enter your first and last name: Hello please enter the following information
and I will compute your Federal Income Tax.

Enter your salary income:

If you supplied some of the equations for calculating the refund amount I'd probably want to create the program for you...its a habit i have :P
Well you could try something like:

Scanner in = new Scanner(System.in);
	System.out.println("Please enter the following details. (If you want to end please type in 0) ");
	input.nextLine();
	
	while(!input.equals(0)){
                // System.out.println("Name and stuff here");
		// Code for tax calculations
		
		System.out.println("Would you like to have another go? If yes, please input the number 1. Otherwise input 0 to exit. ");
		in.nextLine();			//go to the next line.
		input = in.nextLine();
		
	}
	System.out.println("Thanks for that :P");

This is a rough code for it but as long as they don't enter the number zero it will keep looping through. Hope this helps abit.

If you supplied some of the equations for calculating the refund amount I'd probably want to create the program for you...its a habit i have :P
Well you could try something like:

Scanner in = new Scanner(System.in);
	System.out.println("Please enter the following details. (If you want to end please type in 0) ");
	input.nextLine();
	
	while(!input.equals(0)){
                // System.out.println("Name and stuff here");
		// Code for tax calculations
		
		System.out.println("Would you like to have another go? If yes, please input the number 1. Otherwise input 0 to exit. ");
		in.nextLine();			//go to the next line.
		input = in.nextLine();
		
	}
	System.out.println("Thanks for that :P");

This is a rough code for it but as long as they don't enter the number zero it will keep looping through. Hope this helps abit.

THank You very much for taking the time to write back ,,, Im ganna try to implement it now ..

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.