Results 1 to 9 of 9
- 11-17-2011, 10:39 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
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."); } } }
- 11-18-2011, 01:59 AM #2
Member
- Join Date
- Feb 2011
- Location
- 'Mhericuh
- Posts
- 13
- Rep Power
- 0
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:
Using exception handling as a device for a loop might not be the best of style, but it will work.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."); } } } }
Hope this helps.
- 11-18-2011, 12:31 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
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?
-
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?
- 11-18-2011, 06:09 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Interget instead of String input and If Statement re-loop?
Not sure how to do that? Would that be easier?Why not use Scanner's nextInt() rather than next() so you won't even have to parse the input?
Yeh sorry, how would i go about writing a accessor method in the main class?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?
Thanks
-
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.
Certainly you've created setFoo(int foo) and getFoo() methods, right?Yeh sorry, how would i go about writing a accessor method in the main class?
- 11-18-2011, 06:30 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Interget instead of String input and If Statement re-loop?
Okay thanks, ill give that a try.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.
And nope :/ Don't know how to. Ive seen examples of code with 'return variable', would that work?Certainly you've created setFoo(int foo) and getFoo() methods, right?
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:"); } } } }
-
Re: Interget instead of String input and If Statement re-loop?
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; } }
- 11-18-2011, 06:38 PM #9
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Input statement gets skipped
By rjkfsm in forum New To JavaReplies: 2Last Post: 05-15-2011, 07:45 PM -
Help with loop statement
By arvind1508 in forum New To JavaReplies: 2Last Post: 02-23-2011, 04:39 PM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
Need help with a loop statement
By sunshine39 in forum New To JavaReplies: 7Last Post: 11-03-2008, 03:42 AM -
Least To Greates[ if statement and for loop]
By kris09 in forum New To JavaReplies: 1Last Post: 08-08-2008, 06:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks