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

    Unhappy Need help please????

    I have an assignment coming due today by midnight and I have all of the coding correct but I need help with some alternative coding for this section underlined can someone help me please ? We cant use coding that we have not covered in class yet?

    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);
    }

    }
    If you can see I need help with the converting input to an integer withtout using a try and catch coding ? Can you help me please??

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

    Default

    1) "Need help please???" is not a very good subject heading. Think of the subject heading like a newspaper headline: you want to summarize your problem which here is help convert String to int. This tip is being sent in your best interest as the better and more specific the subject heading, the more help you'll likely get, trust me.
    2) You tell us what you can't use here, but what can you use? Can you use methods of the Character class such as "isDigit()"? Character (Java Platform SE 6)
    3) Finally, and again to improve your chances of getting help, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

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

    Default

    Ah, I see that you've already been told about using code tags in your previous thread and have been directed to the forum FAQs. Better late than never. Good luck.

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

    Default

    I forgot to tell you that I am using jGrasp

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

    Default

    Quote Originally Posted by chitwood View Post
    I forgot to tell you that I am using jGrasp
    which has nothing to do with the problem at hand.

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

    Default

    Again what is missing:

    1) code using code tags
    2) specific assignment requirements. why is the current code no good? What can and can't be used? what is necessary? etc...

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

    Default different way to convert user input to an integer ?

    I need a different way to convert the user input to an integer section here is what I have and it works but I cant use what we have not covered in class yet?


    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

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    you while/for loop,
    loop input.trim().length() times
    compare each char in the String with "0", "1", etc ..., if not in [0-9], set the number1 to 0

    remember to times the nth digit with 10 to power n and sum up them

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

    Default

    I can not use a try and catch code I have to use something differnet b/c we have not studied try/catch codes

    Is there any help for me. If you will look I have already underlined the parts for converting number 1 and number 2 to integers and thats the section I need help with?????

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

    Default

    mtyoung has given you an alternative to try/catch. My suggestion is to use the String method toCharArray, then iterate through the array one char at a time and test if each char is a digit via Character.isDigit(...) method. Choice is yours.

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

    Default

    I have not studied this method either so I cannot use this

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

    Default

    That solution is like chapter 6, we are only on chapter 4 so, I cant use arrays yet. Is there something simpler that I can use?

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

    Default

    Then we're kind of at an impasse. We don't know what you've studied, but only you do. I'm kind of stumped how any of us can help you out. Sorry.

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

    Default

    Can you take a look at this website for me and look at Chapter 4 3 2 1 and tell me how to convert this input in the previous post to an Integer without using a try/catch code?

    Introduction to Java Programming, Sixth Edtion

    this is the website to our textbook.

    Thanks,

    Chitwood

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

    Default

    What's wrong with reply #8?

    If you can't use String methods (like trim(), length(), charAt()) you really do have a problem.

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

    Default

    Can you take a look at this website for me and look at Chapter 4,3, 2, 1 and tell me how to convert this input in the previous post to an Integer without using a try/catch code?

    Introduction to Java Programming, Sixth Edtion

    this is the website to our textbook.

    Thanks,

    Chitwood

    I must not be explaining myself correctly to you guys????

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

    Default

    Loops are Chapter 4 and that stuff is later like chapter 16 in this book that I am referring to. There is a more easier way to do it like

    Java Code:
    String number1=Integer.parseInt(?);
    look at the posts earlier, i have underlined the part i need help with.........
    Thanks
    This answer has to be very simple to you more experienced programmers. I am only in the 4th Chapter of the 1st very basic programming book. It cant be that hard to figure out to you guys????? Cmon..

  18. #18
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

Posting Permissions

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