Thread: number to insert twice

1. Member Join Date
Aug 2012
Location
Switzerland
Posts
49
Rep Power
0 number to insert twice

Hi everybody,
I'm sorry, this is the second thread in a few hours, I hope this is a less stupid question than the first...

I'm trying to write a little calculator without any graphic..here's the code

Java Code:
package somma2;

import java.util.Scanner;
import static java.lang.System.out;
import static java.lang.System.in;

public class Somma2 {

public static void main(String[] args) {
double primoNumero, secondoNumero, terzoNumero, quartoNumero, quintoNumero, somma;

out.print("wich operation would you like to do? insert + - * / ");
Scanner operazione = new Scanner(in);

if (operazione.findInLine(".").charAt(0) == '+') {
out.println("How many numbers would you like to add? Insert a number from 2 to 5 ");
Scanner numeriSomma = new Scanner(in);

if (numeriSomma.nextInt() == 1) {
out.print("You have to insert a number beetwen 2 and 5");
}

if (numeriSomma.nextInt() == 2) {
out.print("Insert the numbers here -> ");
Scanner so2n = new Scanner(in);
primoNumero = so2n.nextDouble();
secondoNumero = so2n.nextDouble();
somma = primoNumero + secondoNumero;
out.print(primoNumero);
out.print(" + ");
out.print(secondoNumero);
out.print(" = ");
out.println(somma);

}

}

}
}
the problem is: when it asks "How many numbers would you like to add?" if I put 1, no problems, If I put 2, I have to write it two times to make it work! I mean that I have to write "2", then press enter, then insert "2" again, to make it ask the two numbers...

thank you in advance
Chri  Reply With Quote

2. Re: number to insert twice

A comment on the way the code is written, I recommend that you read the values into a variable and test the content of that variable vs doing it in one step:
if (numeriSomma.nextInt() == 1) {
vs
int nbrIn = numeriSomma.nextInt();
if (nbrIn == 1) {

You are probably having a problem with the Scanner class leaving the newline character in its buffer when you use the nextInt() method.  Reply With Quote

3. Member Join Date
Aug 2012
Location
Switzerland
Posts
49
Rep Power
0 Re: number to insert twice

same...I have to insert it twice

EDIT: now it works! but I can't figure out how...  Reply With Quote

4. Re: number to insert twice

What did you change?
Did you make the change I recommended: read into a variable?  Reply With Quote

5. Member Join Date
Aug 2012
Location
Switzerland
Posts
49
Rep Power
0 Re: number to insert twice

yes I did what you recommended, i changed if (numeriSomma.nextInt() == 1) { with
nt nbrIn = numeriSomma.nextInt();
if (nbrIn == 1) {  Reply With Quote

6. Member Join Date
Aug 2012
Posts
5
Rep Power
0 Re: number to insert twice

il problema è stato che ogni volta tu metti numeriSomma.nextInt() lui va a leggere un nummero. cio è con numeriSomma.nextInt() == 1 leggi il primo nummero. poi numeriSomma.nextInt() == 2 legge il secondo nummero.  Reply With Quote

7. Re: number to insert twice

In English please; English is the lingua franca here.

kind regards,

Jos  Reply With Quote

8. Member Join Date
Aug 2012
Location
Switzerland
Posts
49
Rep Power
0 Re: number to insert twice Originally Posted by ftftftftftftftft il problema è stato che ogni volta tu metti numeriSomma.nextInt() lui va a leggere un nummero. cio è con numeriSomma.nextInt() == 1 leggi il primo nummero. poi numeriSomma.nextInt() == 2 legge il secondo nummero.
Grazie mille :D
but I can't figure out yet why just assigning a variable instead of using directly numeriSomma.nextInt, it works...
OT: you're not Italian right? Anyway, nice! (Hoping you did not use google translate hehe)

for every english speaking people who may have the same problem, here's the answer in english: Originally Posted by ftftftftftftftft the problem was that each time you put numeriSomma.nextInt() it reads a number. so with numeriSomma.nextInt() == 1 it reads the first number. then with numeriSomma.nextInt() == 2 it reads the second number.  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•