Results 1 to 15 of 15
  1. #1
    Jakora33 is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default Inputting custom numbers

    Hi I'm new to java and I just put together this program. I was wondering how I could modify this prompt to make numberThree a custom number of the user's choosing. Thanks!

    class Addition{
    public static void main(String[] args){

    int numberOne, numberTwo, numberThree, total;
    numberOne = 10;
    numberTwo = 5;
    numberThree=2;

    total = numberOne + numberTwo + numberThree;
    System.out.print("The number is " + total);
    }
    }

  2. #2
    anilanar is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    You can use TextIO to ask input from the user. Google it and put it in the folder you use for your java files.

    Java Code:
    class Addition{
    public static void main(String[] args){
    
    int numberOne, numberTwo, numberThree, total;
    numberOne = 10;
    numberTwo = 5;
    [B]numberThree = TextIO.getln();[/B]
    
    total = numberOne + numberTwo + numberThree;
    System.out.print("The number is " + total);
    }
    }

  3. #3
    Jakora33 is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    How would I modify the code to have something that says "Please put the number in here," and then allow the user to type that in.

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    11

    Default

    You'd be better off using something like Scanner to get input. TextIO is non-standard, but if you want something like that, try the Console class. I suggest Scanner because Console has a tendency not to work with IDEs.

    Scanner example:
    Java Code:
    import java.util.Scanner;
    import java.util.InputMismatchException;
    class Example{
    
    public static void main(String[] args){
         int total, one=10, two=5, three;
         Scanner in = new Scanner(System.in);
         System.out.print("Enter number 3: ");
         try{
              three = Scanner.nextInt();
         }catch(InputMismatchException ime){
              System.err.println("The given input was not an integer");
              return;//terminate program
         }
         total = one+two+three;
         System.out.println("Number One = " + one);
         System.out.println("Number Two = " + two);
         System.out.println("Number Three = " + three);
         System.out.println("Total = " + total);
    }
    
    }
    //DISCLAIMER: This code was written in the quick reply box, and may not work :D
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    Loop is offline Member
    Join Date
    Sep 2008
    Posts
    36
    Rep Power
    0

    Default

    Do you mean a primitive GUI?

    In that case
    Java Code:
    import javax.swing.*;//top of program
    
    //Now, use this for number 3:
    
    String sThree = JOptionPane.showInputDialog("Please put the number in here");
    numberThree = Integer.parseInt(sThree);
    Tell me if that is what you are looking for.
    www.talkbinary.com - for anyone that wants to get a new C++ forum on its feet.;)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

  7. #7
    Loop is offline Member
    Join Date
    Sep 2008
    Posts
    36
    Rep Power
    0

    Default

    How would I modify the code to have something that says "Please put the number in here," and then allow the user to type that in.
    Sounds like the poster wanted a primitive GUI to me.
    www.talkbinary.com - for anyone that wants to get a new C++ forum on its feet.;)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    Quote Originally Posted by Loop View Post
    Sounds like the poster wanted a primitive GUI to me.
    Hm, curious. Where did you see a statement suggesting that?

  9. #9
    Jakora33 is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default Trying this out

    I tried this one, but I get the error below

    "cannot find symbol variable TextIO"

    How can I fix this?


    import java.util.Scanner;

    class Addition{
    public static void main(String[] args){

    int numberOne, numberTwo, numberThree;
    numberOne = 10;
    numberTwo = 5;
    numberThree = 2;
    int numberFour;

    System.out.println("Please add another number: ");

    numberFour = TextIO.getInt();
    int total = numberOne + numberTwo + numberThree + numberFour;




    System.out.print("The number is");
    }
    }

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    13

    Default

    Throw away that TextIO thing and see the API specs for Scanner.

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    11

    Default

    Quote Originally Posted by r035198x View Post
    Throw away that TextIO thing and see the API specs for Scanner.
    Thats the same thing I suggested... TextIO is not part of the JDK so it's best to avoid it.
    Last edited by Singing Boyo; 09-07-2009 at 11:21 PM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  12. #12
    Loop is offline Member
    Join Date
    Sep 2008
    Posts
    36
    Rep Power
    0

    Default

    Jakora33, why don't you try this:

    Java Code:
    import javax.swing.*;
    
    class Addition
    {
    public static void main(String[] args)
    {
    
    int numberOne, numberTwo, numberThree;
    numberOne = 10;
    numberTwo = 5;
    numberThree = 2;
    
    String sFour = JOptionPane.showInputDialog("Please add another number:");
    
    int numberFour = Integer.parseInt(sFour);
    
    int total = numberOne + numberTwo + numberThree + numberFour;
    
    System.out.print("The Total is " total);
    }
    }
    www.talkbinary.com - for anyone that wants to get a new C++ forum on its feet.;)

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    13

    Default

    Quote Originally Posted by Loop View Post
    Jakora33, why don't you try this:

    Java Code:
    import javax.swing.*;
    
    class Addition
    {
    public static void main(String[] args)
    {
    
    int numberOne, numberTwo, numberThree;
    numberOne = 10;
    numberTwo = 5;
    numberThree = 2;
    
    String sFour = JOptionPane.showInputDialog("Please add another number:");
    
    int numberFour = Integer.parseInt(sFour);
    
    int total = numberOne + numberTwo + numberThree + numberFour;
    
    System.out.print("The Total is " total);
    }
    }
    Rather let them learn to use Scanner first.
    You also need to learn to define variables closest to where they are used.
    Java Code:
    int numberOne, numberTwo, numberThree;
    numberOne = 10;
    numberTwo = 5;
    numberThree = 2;
    is the old (bad) C style of programming.
    Java Code:
    int numberOne = 10;
    Also learn to format your code.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    As discussed, let's agree to avoid adding unnecessary complexity such is caused by premature use of Swing until the OP can handle standard I/O first.

  15. #15
    Loop is offline Member
    Join Date
    Sep 2008
    Posts
    36
    Rep Power
    0

    Default

    Yeah, sorry. Guess he should learn it first.
    www.talkbinary.com - for anyone that wants to get a new C++ forum on its feet.;)

Similar Threads

  1. Reversing and inputting files
    By jigglywiggly in forum New To Java
    Replies: 14
    Last Post: 03-23-2009, 09:28 AM
  2. inputting and writing a file backwards
    By jigglywiggly in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 08:24 PM
  3. Replies: 2
    Last Post: 12-06-2008, 07:54 PM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 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
  •