0

So i made a calc a while ago ,

now im trying to chance some stuff , instead of using 3 scanners i need it to use 1 only but really cant figure out how :S

and at the end of the code where it asks the user for J to continue and N to finish i want to put that in a Do while loop but its not working any help is appreciated!

Code atm looks like this användarInlägg is = userinput , and yes im new to this

boolean status=true;
while (status){
Scanner minScanner1 = new Scanner(System.in);  
Scanner scanner2 = new Scanner(System.in);
System.out.println("Skriv in ditt nummer en och en :\n");     
double nr1 = minScanner1.nextDouble(); 
double nr2 = minScanner1.nextDouble();


System.out.println("välj vad du vill göra: ");

double svr =0;

String användarInlägg = scanner2.nextLine();
if(användarInlägg.equalsIgnoreCase("+")) { 
    svr = nr1 + nr2;
    }
else if(användarInlägg.equalsIgnoreCase("-")) {
    svr = nr1 - nr2;
    }
else if(användarInlägg.equalsIgnoreCase("*")) {
    svr = nr1 * nr2;
    }
else if(användarInlägg.equalsIgnoreCase("/")) {
    svr = nr1 / nr2;
    }
System.out.printf("= %.2f\n ", svr);
 System.out.println("Skriv in J för att fortsätta N för att sluta: \n"); 

 Scanner scanner3 = new Scanner(System.in);
    String input=scanner3.nextLine();

    do{
    }while(input.equalsIgnoreCase("J"));
    if (input.equalsIgnoreCase("N")) {
       System.exit(0);
    } else if (input.equalsIgnoreCase("J")) {
        status = true;
    }
}
 }   

}

Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
user2764700
  • 29
  • 3
  • 3
  • 7
  • I am pretty sure you are having the issue described in this post - http://stackoverflow.com/a/13102066/1679863 – Rohit Jain Sep 19 '13 at 08:33
  • Whatever is explained there for `Scanner#nextInt()` still holds true for `Scanner#nextDouble()` too. – Rohit Jain Sep 19 '13 at 08:33
  • the problem is that i need 1 scanner instead of 3 and do while to reboot program to get 2 new numbers from user or shutdown not sure why that link u gave has something with this to do? – user2764700 Sep 19 '13 at 08:35
  • That empty `do-while` loop is doing no good out there. You need to re-read the user input inside ti. – Rohit Jain Sep 19 '13 at 08:35
  • Ok so what issues did you get while using 1 Scanner instead of 3? You haven't talked about that. – Rohit Jain Sep 19 '13 at 08:36
  • then the program asks write in 2 numbers and then gives the answere 0.00 without letting u type in what kind of calc u wanna do – user2764700 Sep 19 '13 at 08:37
  • Exactly. This is the same issue as described in that post. Go to that post and check to see if you can figure out what went wrong. – Rohit Jain Sep 19 '13 at 08:38

3 Answers3

0
 if (input.equalsIgnoreCase("J")) {
 do
{
//body or call
}
while(input.equalsIgnoreCase("J"));
    } 
else if (input.equalsIgnoreCase("N")) {
         System.exit(0);
    }
Kanwaljit Singh
  • 4,220
  • 2
  • 16
  • 21
0

First of all, you have to make only one Scanner object. You dont need Scanner2 and Scanner 3.. All the scanner objects point to the same input stream and take values from keyBoard.

You can use miniScanner1.nextInt() , miniScanner1.nextLine() etc..

Coming to your doWhile loop.. What exactly do u want to achieve in your doWhile loop??

TheLostMind
  • 34,842
  • 11
  • 64
  • 97
  • i want my whileloop to if the user types J go back to line " type in 2 numbers to calc " if user types in N just shut the prog down – user2764700 Sep 19 '13 at 08:50
0

2 Problems:

1: String input=scanner3.nextLine(); will consume the newLine character. look for: stackoverflow.com/a/13102066/1679863

2:

do{
}while(input.equalsIgnoreCase("J"));

is infinite loop if input equals "J". I guess you want to repeat the calculation if user enter "J" so change your:

while(status){
...
do{
}while(input.equalsIgnoreCase("J"));
if (input.equalsIgnoreCase("N")) {
   System.exit(0);
} else if (input.equalsIgnoreCase("J")) {
    status = true;
}
}

to:

while (status){
    ...
    if (input.equalsIgnoreCase("N"))
       status = false;
}
Community
  • 1
  • 1
JohnnyAW
  • 2,716
  • 1
  • 11
  • 24