Results 1 to 4 of 4
  1. #1
    chitwood is offline Member
    Join Date
    Jan 2008
    Posts
    23
    Rep Power
    0

    Default input to an integer (simply)

    I need help with this basic program. I am trying to convert an input to an integer but I cant use the way I have it written in this coding method. It has to be something simpler. Is there a solution to my problem?

    Java Code:
    import javax.swing.JOptionPane;
    
        public class Assignment4ITEC2260
       {
       
           public static void main(String[] args)
          {
             int number1;
             int number2;
             int num;
          
          
          
          // Prompt for first number
             do{
                String input = JOptionPane.showInputDialog(null,"Enter first number");
                if(input == null) 
                   System.exit(1);
             
             // convert input to a integer
                try
                {
                   number1 = Integer.parseInt(input.trim());
                }
                    catch(NumberFormatException ex)
                   {
                      number1 = 0;
                   }
             }while(number1 < 1);
          
          // Prompt for second number
             do{
                String input = JOptionPane.showInputDialog
                   (null,"Enter second number greater than first number");
                if(input == null) 
                   System.exit(1);
             
             // convert input to a integer
                try
                {
                   number2 = Integer.parseInt(input.trim());
                }
                    catch(NumberFormatException ex)
                   {
                      number2 = 0;
                   }
             }while(number2 < number1);
          
          
          // display the odd numbers between the first number and the second number.
          
             String numbers = "";
          
          // using a for loop
             for(num = number1+1; num < number2;num++)
             {
             // check for odd number
                if((num % 2) != 0)
                {
                   numbers = numbers + num + " ";
                }
             }
          
             JOptionPane.showMessageDialog
                (null,numbers,"using for loop", JOptionPane.INFORMATION_MESSAGE);
          
          // using a do loop
             numbers = "";
             num = number1+1;
          
             do{
             
             // check for odd number
                if((num % 2) != 0)
                {
                   numbers = numbers + num + " ";
                }
             
                num++;
             
             } while(num < number2);
          
          
             JOptionPane.showMessageDialog
                (null,numbers,"using a do loop", JOptionPane.INFORMATION_MESSAGE);
          
          // using a do-while loop
          
             numbers = "";
             num = number1+1;
          
             while(num < number2)
             {
             
             // check for odd number
                if((num % 2) != 0)
                {
                   numbers = numbers + num + " ";
                }
             
                num++;
             
             } 
          
             JOptionPane.showMessageDialog
                (null,numbers,"using a do-while loop", JOptionPane.INFORMATION_MESSAGE);
          
          
             System.exit(0);
          }
       
       }
    Thanks,
    Chitwood

  2. #2
    chitwood is offline Member
    Join Date
    Jan 2008
    Posts
    23
    Rep Power
    0

    Default

    I cant use try/catch in number1 and number2 conversions? Does anyone have an easy solution?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Cross-posted from new to Java: Need help please????

    Moderators, please dispose of this thread.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. Replies: 6
    Last Post: 10-17-2008, 06:11 PM
  2. Error With Simply Displaying Picture
    By JDCAce in forum Java Applets
    Replies: 4
    Last Post: 10-07-2008, 08:09 PM
  3. Integer vs int
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-14-2007, 09:13 PM
  4. Simply output the result to a text file.
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:48 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08: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
  •