Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Integer.parseInt problem

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Integer.parseInt problem

    Hi guys, working through the Deitel Java book and am stumped on one of the cases they give, with no examples, I've tried Googling around but with no success.
    The program is just a simple GUI to solve a math problem but the task mentions using the parseInt functio which I can't seem to get to work.
    Below is kind of what I've been trying...

    Any help much appreciated

    import javax.swing.JOptionPane;
     
    public class Exercise
     
    {
     
    public static void main( String[] args )
     
    {
     
    int	number1;
    int	number2;
    int	sum;
     
     
    String number1 = JOptionPane.showInputDialog( "Enter first number:" );
    //number1 = Integer.parseInt();
     
    String number2 = JOptionPane.showInputDialog( "Enter second number:" );
    //number2 = Integer.parseInt();
     
    sum = number1 + number2;
     
    String message = String.format( "Sum of numbers is %d", sum );
     
    JOptionPane.showMessageDialog( null, message );
     
    }
     
    }


  2. #2
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    My Mood
    Relaxed
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Integer.parseInt problem

    1. You can't have both an integer and a String called number1, same for number2.

    2. Integer.parseInt(); takes parameters, which means you must give the method something to parse into an int.

    3. Why are you using the String message? You can put whatever you like directly into the message dialog.

    If these tips don't help you out, let me know, I'll explain further. However, keep in mind that figuring it out yourself means you understand it better and you will remember it.

    edit: You should really be putting try catch statements on your input dialogues, because if I entered letters into them, your program would crash.

  3. The Following User Says Thank You to Patient For This Useful Post:

    3therk1ll (June 28th, 2013)

  4. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Integer.parseInt problem

    Thanks for the reply dude.
    I am using the string message because I thought the idea was to read a string and filter out the integers from that string.
    Not using catch because I'm just working my way through the Deitel Java Programming book and haven't been introduced to catch statements yet, the exercise was to take a previous basic math command line program and convert it to be used with a basic GUI.
    Java is still really new to me, I've only ever scripted before with Bash.

    I think I have it a bit more figured out now, but I am still doing something wrong as I get an error:

    C:\Program Files\Java\jdk1.7.0_21\bin>javac C:\Java\Exercise.java
    C:\Java\Exercise.java:20: error: incompatible types
    sum = number1 + number2;
                  ^
      required: int
      found:    String
    1 error

    This is my code thus far, I assume that it is still expecting a string and won't do addition with two strings, something wrong with how I'm parsing?
    I tried changing String number1 and 2 to int but just got more errors.

    import javax.swing.JOptionPane;
     
    public class Exercise
     
    {
     
    public static void main( String[] args )
     
    {
     
    int	sum;
     
     
    String number1 = JOptionPane.showInputDialog( "Enter first number:" );
    Integer.parseInt( number1 );
     
    String number2 = JOptionPane.showInputDialog( "Enter second number:" );
    Integer.parseInt( number2 );
     
    sum = number1 + number2;
     
    String message = String.format( "Sum of numbers is %d", sum );
     
    JOptionPane.showMessageDialog( null, message );
     
    }
     
    }

  5. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Integer.parseInt problem

    parseInt returns what? Do you expect it to automagically transform a String into an int?

  6. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Integer.parseInt problem

    I thought the point of it was to parse a string for integers.

  7. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Integer.parseInt problem

    Yes, and what happens with the parsed int?

  8. The Following User Says Thank You to PhHein For This Useful Post:

    3therk1ll (June 28th, 2013)

  9. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Integer.parseInt problem

    Urgh, I'm a dick, I got it now, cheers for the help, I wasn't declaring turning the parsed string into another variable and declaring it as an int.

    String number1 = JOptionPane.showInputDialog( "Enter first number:" );
    int number1a = Integer.parseInt( number1 );

    That worked.

  10. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Integer.parseInt problem

    No problem Don't forget the Exception handling, as has been mentioned before.

  11. #9
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Integer.parseInt problem

    Urgh, I'm a dick, I got it now, cheers for the help, I wasn't declaring turning the parsed string into another variable and declaring it as an int.

    String number1 = JOptionPane.showInputDialog( "Enter first number:" );
    int number1a = Integer.parseInt( number1 );

    That worked.

    --- Update ---

    Yeah ok thanks dude.
    Sorry about the double post, the page won't let me edit my posts, tried to repost with code tags.

Similar Threads

  1. Interger.parseInt Question
    By Tedstriker in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 6th, 2013, 11:46 AM
  2. HELP: I have problem casting from Vector to Integer in Java
    By tintin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 17th, 2011, 12:39 PM
  3. Use of Integer.parseInt
    By tarkal in forum Java SE APIs
    Replies: 3
    Last Post: September 8th, 2011, 03:37 AM
  4. Using Integer.parseInt
    By Fraz in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 07:50 AM
  5. Hex to Integer Problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2010, 06:01 AM