Results 1 to 8 of 8
Thread: number to insert twice
- 08-03-2012, 08:43 PM #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
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...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); } } } }
thank you in advance
Chri
- 08-03-2012, 08:52 PM #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.If you don't understand my response, don't ignore it, ask a question.
- 08-03-2012, 09:13 PM #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...
- 08-03-2012, 09:41 PM #4
Re: number to insert twice
What did you change?
Did you make the change I recommended: read into a variable?If you don't understand my response, don't ignore it, ask a question.
- 08-03-2012, 10:00 PM #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) {
- 08-03-2012, 10:00 PM #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.
- 08-03-2012, 10:36 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: number to insert twice
In English please; English is the lingua franca here.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-03-2012, 11:49 PM #8
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
Re: number to insert twice
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:
Similar Threads
-
Easy Question! Print out file, count number of lines, number of spaces w/ Scanner
By LogicalOutlier in forum New To JavaReplies: 8Last Post: 01-21-2012, 01:14 AM -
Dice help. posting the number of times a number is rolled.
By cookiejarvus in forum New To JavaReplies: 13Last Post: 12-04-2011, 11:08 PM -
How to insert random number to database
By waboke in forum New To JavaReplies: 1Last Post: 09-24-2010, 04:45 PM -
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02-05-2010, 10:42 PM -
How to insert large data into database using one insert query
By sandeepsai39 in forum New To JavaReplies: 3Last Post: 02-28-2009, 09:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks