Results 1 to 8 of 8
  1. #1
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default 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.

  3. #3
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: number to insert twice

    same...I have to insert it twice

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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default 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.

  5. #5
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default 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) {

  6. #6
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default 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.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: number to insert twice

    In English please; English is the lingua franca here.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: number to insert twice

    Quote Originally Posted by ftftftftftftftft View Post
    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:


    Quote Originally Posted by ftftftftftftftft View Post
    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.

Similar Threads

  1. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  2. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-04-2011, 11:08 PM
  3. How to insert random number to database
    By waboke in forum New To Java
    Replies: 1
    Last Post: 09-24-2010, 04:45 PM
  4. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM
  5. Replies: 3
    Last Post: 02-28-2009, 09:17 AM

Posting Permissions

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