Saturday, 15 March 2014

java - Why is this line printed twice? -



java - Why is this line printed twice? -

i have created programme part of computing work checking stock , works fine however, after user has gone through programme 1 time checking stock of 1 item, asked if check stock of item same line printed twice , can't figure out why?. code:

import java.util.*; public class stock { public static void main(string[] args) { //initialising scanners scanner stock = new scanner(system.in); scanner levels = new scanner(system.in); scanner bar = new scanner(system.in); scanner selection = new scanner(system.in); //initialising string variables string chocolate; string chocolate2; string change; string choiceb; //initialising integer variables int mars = 200; int twix = 200; int bounty = 200; int doubled = 200; int galaxy = 200; int change2; int counter = 1; int = 1; //asking user chocolate bar want check stock of system.out.println("enter chocolate bar check stock of: mars, twix, bounty, double , galaxy"); system.out.println("***********************************"); chocolate = stock.nextline(); system.out.println("***********************************"); //depending on users choice, switch statement outputs appropriate stock level of bar entered switch (chocolate.tolowercase()) { case ("mars"): system.out.println("there currenty " + mars + " in stock"); break; case ("twix"): system.out.println("there currenty " + twix + " in stock"); break; case ("bounty"): system.out.println("there currenty " + bounty + " in stock"); break; case ("double"): system.out.println("there currenty " + doubled + " in stock"); break; case ("galaxy"): system.out.println("there currenty " + galaxy + " in stock"); break; default: system.out.println("your idiot, seek again"); chocolate = stock.nextline(); } //the user asked if want alter stock level of of chocolate bars system.out.println("do want alter stock levels?"); system.out.println("***********************************"); alter = levels.nextline(); system.out.println("***********************************"); //if reply yes carries on programme , ignores if statement. if reply no, programme closes if (change.equals("no")) { system.exit(0); } //this while loop , switch statement used check chocolate bar stock level user wants change. 1 subtracted counter // on users first input message of checking if user wants alter more appears. while (a == 1){ if (counter == 0) { system.out.println("do want alter stock of more"); choiceb = choice.nextline(); counter = counter + 1; }else{ system.out.println("which chocolate want alter stock levels of?"); system.out.println("***********************************"); chocolate2 = bar.nextline(); system.out.println("***********************************"); switch (chocolate2.tolowercase()) { case ("mars"): system.out.println("enter amount of mars bars in stock"); mars = bar.nextint(); system.out.println("there " + mars + " in stock"); counter = counter - 1; break; case ("twix"): system.out.println("enter amount of twix in stock"); twix = bar.nextint(); system.out.println("there " + twix + " in stock"); counter = counter - 1; break; case ("bounty"): system.out.println("enter amount of bounty bars in stock"); bounty = bar.nextint(); system.out.println("there " + bounty + " in stock"); counter = counter - 1; break; case ("double"): system.out.println("enter amount of double bars in stock"); doubled = bar.nextint(); system.out.println("there " + doubled + " in stock"); counter = counter - 1; break; case ("galaxy"): system.out.println("enter amount of galaxy in stock"); galaxy = bar.nextint(); system.out.println("there " + galaxy + " in stock"); counter = counter - 1; break; } } } } }

this output when programme ran:

the problem mix of reading lines , integers:

system.out.println("which chocolate want alter stock levels of?"); system.out.println("***********************************"); chocolate2 = bar.nextline(); system.out.println("***********************************"); switch (chocolate2.tolowercase()) { case ("mars"): system.out.println("enter amount of mars bars in stock"); mars = bar.nextint();

first, reading bar using nextline(). user come in mars\r\n (\r\n linebreak caused hitting return) , scanner reads mars\r\n

then reading nextint() (!) bar. user enters 2\r\n, nextint() read 2, leaving \r\n on bar scanner, cursor just ahead of \r\n.

your logic enters second loop, reprints message, when bar scanner hits nextline() again, proceed , read \r\n - switch fails on , logic enters 3rd loop (message printed sec time)

now, bar empty again, bar.readline() wait user input again.

to prepare issue, create sure skip current line, after reading integer, when scanner hits nextline() 1 time again while prompting type not consume linebreak.:

mars = bar.nextint(); bar.nextline();

java

No comments:

Post a Comment