Results 1 to 9 of 9
  1. #1
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default Interget instead of String input and If Statement re-loop?

    Im in the process of writing a pipe length class using netbeans and have occurred 2 hurdles.

    The input by the user MUST be a integer, if the user enters a string or other character, it crashes, how can i solve this?
    Also, if the criteria is NOT met for the length, if says please enter another value however i do not know how to re-loop so it asks for the length again.

    All help will be greatly appreciated.

    Thanks.

    Java Code:
    import java.util.*;
    
    /**
     *
     * @author Parik
     */
    public class checkLength 
    {
        int userLength;
        
        public checkLength()
        {
            Scanner in = new Scanner(System.in);
            System.out.println("What would you like the length of the pipe to be? (cm)");
            userLength = in.nextInt();
    
            if (userLength > 9 && userLength < 501)
                {                     
                    System.out.println("Your required length of pipe: " + userLength + "cm");
                }
            else if (userLength > 500)
            {
                System.out.println("The pipe length exceeds our max length by: " + (userLength - 500) + "cm");
                System.out.println("Please enter another value.");
            }
            else if (userLength < 10)
            {
                System.out.println("The pipe length does not meet our minimum requirements by: " + (10 - userLength) + "cm");
                System.out.println("Please enter another value.");
            }
        }
    
    }

  2. #2
    zirbinator is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default Re: Interget instead of String input and If Statement re-loop?

    The simplest way I have seen to read an integer from an input like this is to read it as a String and then convert to an integer with Integer.parseInt(String string). I have never used the Scanner class before, but I think some code would look like this:

    Java Code:
    import java.util.*;
    
    public class CheckLength 
    {
        int userLength;
        
        public static void main(String[] args)
        {
        	new CheckLength();
        }
         
        public CheckLength()
        {
            Scanner in = new Scanner(System.in);
            System.out.println("What would you like the length of the pipe to be? (cm)");
            while(true)
            {
                String userLengthString = in.next();
                try
                {
                	userLength = Integer.parseInt(userLengthString);
                	if (userLength > 9 && userLength < 501)
                    {
                        System.out.println("Your required length of pipe: " + userLength + "cm");
                        break;
                    }
                    else if (userLength > 500)
                    {
                        System.out.println("The pipe length exceeds our max length by: " + (userLength - 500) + "cm");
                        System.out.println("Please enter another value.");
                    }
                    else if (userLength < 10)
                    {
                        System.out.println("The pipe length does not meet our minimum requirements by: " + (10 - userLength) + "cm");
                        System.out.println("Please enter another value.");
                    }
                }
                catch (NumberFormatException ex) 
                {
                	System.out.println("Please enter an integer.");
                }
            }
        }
    }
    Using exception handling as a device for a loop might not be the best of style, but it will work.

    Hope this helps.

  3. #3
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default Re: Interget instead of String input and If Statement re-loop?

    Hi that worked perfectly, thanks!

    Just 1 more thing, how could i return the interger so it can later be used when called in the main method?

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

    Default Re: Interget instead of String input and If Statement re-loop?

    Why not use Scanner's nextInt() rather than next() so you won't even have to parse the input?

    As for how to "return the integer"? Return it where? this is a class constructor and nothing gets returned from it. Perhaps you wish to give this class a getter or accessor method?

  5. #5
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default Re: Interget instead of String input and If Statement re-loop?

    Why not use Scanner's nextInt() rather than next() so you won't even have to parse the input?
    Not sure how to do that? Would that be easier?

    As for how to "return the integer"? Return it where? this is a class constructor and nothing gets returned from it. Perhaps you wish to give this class a getter or accessor method?
    Yeh sorry, how would i go about writing a accessor method in the main class?

    Thanks

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

    Default Re: Interget instead of String input and If Statement re-loop?

    Quote Originally Posted by FadedAura View Post
    Not sure how to do that? Would that be easier?
    It could be. Instead of calling next() on your Scanner variable, you'd call nextInt() when you know for certainty that int is required and will be entered.


    Yeh sorry, how would i go about writing a accessor method in the main class?
    Certainly you've created setFoo(int foo) and getFoo() methods, right?

  7. #7
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default Re: Interget instead of String input and If Statement re-loop?

    It could be. Instead of calling next() on your Scanner variable, you'd call nextInt() when you know for certainty that int is required and will be entered.
    Okay thanks, ill give that a try.
    Certainly you've created setFoo(int foo) and getFoo() methods, right?
    And nope :/ Don't know how to. Ive seen examples of code with 'return variable', would that work?

    My current code is:

    Java Code:
    import java.util.*;
    
    /**
     *
     * @author Parik
     */
    public class checkLength 
    {
        int userLength, length;
        
        public checkLength()
        {
            Scanner in = new Scanner(System.in);
            System.out.println("What would you like the length of the pipe to be? (cm)");
            
            while (true)
            {
                String userLengthString = in.next();
                try{
                    userLength = Integer.parseInt(userLengthString);
                    if (userLength > 9 && userLength < 501)
                    {                     
                        System.out.println("Your required length of pipe is " + userLength + "cm");
                        break;
                    }
                    else if (userLength > 500)
                    {
                        System.out.println("The pipe length exceeds our max length by " + (userLength - 500) + "cm");
                        System.out.println("Please enter another value.");
                    }
                    else if (userLength < 10)
                    {
                        System.out.println("The pipe length does not meet our minimum requirements by " + (10 - userLength) + "cm");
                        System.out.println("Please enter another value:");
                    }
                    }
                catch (NumberFormatException ex)
                    {
                        System.out.println("Please enter an integer:");
                    }
            }
        }
    }

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

    Default Re: Interget instead of String input and If Statement re-loop?

    Quote Originally Posted by FadedAura View Post
    And nope :/ Don't know how to. Ive seen examples of code with 'return variable', would that work?
    Give it a try and see. :)

    An example of getter and setter variables:

    Java Code:
    public class GettersAndSetters {
       private int fu;
       private int bar;
       
       
       public int getFu() {
          return fu;
       }
       public void setFu(int fu) {
          this.fu = fu;
       }
       public int getBar() {
          return bar;
       }
       public void setBar(int bar) {
          this.bar = bar;
       }
    }

  9. #9
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default Re: Interget instead of String input and If Statement re-loop?

    Thanks! Worked. Much appreciated.

Similar Threads

  1. Input statement gets skipped
    By rjkfsm in forum New To Java
    Replies: 2
    Last Post: 05-15-2011, 08:45 PM
  2. Help with loop statement
    By arvind1508 in forum New To Java
    Replies: 2
    Last Post: 02-23-2011, 05:39 PM
  3. Replies: 5
    Last Post: 01-11-2009, 11:12 AM
  4. Need help with a loop statement
    By sunshine39 in forum New To Java
    Replies: 7
    Last Post: 11-03-2008, 04:42 AM
  5. Least To Greates[ if statement and for loop]
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-08-2008, 07:34 PM

Tags for this Thread

Posting Permissions

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