• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ne chance of a little help?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay heres the problem, im designing this greenhouse simulator, using java (of course!) but i need it to quite the program when ever the user enters "q" instead of the normal temperature, humidity or draught speed. I thought about transforming a string input to a integer but i dont know exactly how to go about it.
Ne help would be much appriciated.
Tnx
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I thought about transforming a string input to a integer...

Rather than go through that trouble, just compare the input to see if equals "q" (or 'q', depending on how you're obtaining the input).

Please read this: http://faq.javaranch.com/view?UseAMeaningfulSubjectLine

Also, please make the extra effort to write out words such as "any" and "thanks". The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership.

Cheers!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you check if the input string equals "q", and quit if that's the case? (I gather that it's a command-line application, because with a GUI you'd just add a "Quit" button or menu.)
Once you know it's not "q" you can convert the input string to a number, e.g. using Integer.valueOf() or Double.valueOf().
 
Daniel Ryan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks guys, using this Integer.valueOf() statement the string goes in the brakets right? so it would look somthing like
desirednumber = Integer.valueOf(String Userinput)
Is that about right??
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

desirednumber = Integer.valueOf(String Userinput)
Is that about right??


If "desirednumber" is an Integer object, yes. If you want an int value, use Integer.parseInt().
 
author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget about exception handling: if you try to parse a non-integer value, a NumberFormatException will be thrown that you can catch; then, by looking at the message that the exception is carrying (which, in the case of a NumberFormatException, will be the non-int value itself), you can determine if it's time to hang up your saddle!

Regards,

Jacquie

P.S. You had the "String" type inside of your code example:

Integer.parseInt(String inputVal)

which is incorrect -- when CALLING a method such as parseInt, you don't specify the type of the argument you are passing; you only declare the type of a parameter when the method is declared (something that, in this case, happens for you within the Integer class).
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

In your haste to come in and ask a question, you seem to have missed reading our policy on display names, which quite clearly states that you must use a real (sounding) first and last name for your display name -- no joke names, "handles," or last initials are acceptable. You can fix your display namehere. Thanks for your cooperation!
 
Daniel Ryan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks guys this is a real help, only one more problem, i want to return the desiredinput but it keeps saying its not initialised. Any way hers the code see if you can spot the errors
public int getTemp()
{
String t;
int temp;

System.out.println ("What is the current temperature in degrees?");
t = Reader.readLine();
try {
temp = Integer.parseInt(t);
}
catch (NumberFormatException e) {
if (e.getMessage().equalsIgnoreCase("q")) {
System.exit(0);
}
}
return temp;
}
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "temp" variable will need to be initialized...
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

P.S. You had the "String" type inside of your code example:

Integer.parseInt(String inputVal)

which is incorrect -- when CALLING a method such as parseInt, you don't specify the type of the argument you are passing; you only declare the type of a parameter when the method is declared (something that, in this case, happens for you within the Integer class).

Nice catch - I overlooked that completely. On a side note, is depending on the NFE exception message for evaluating user input a good idea?
 
Daniel Ryan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thoght it was initialised temp = Integer.parseInt(t)..etc....
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Daniel Ryan:
i thoght it was initialised temp = Integer.parseInt(t)..etc....


Say that line throws an exception, and you get into the catch block. Then, if e.getMessage().equalsIgnoreCase("q") evaluates to false, you'll reach the "return temp" line. The "temp" variable will have no value - it hasn't been initialized, because parseInt() threw an exception and therefore no value was assigned.
 
Daniel Ryan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah i see, well hears a slightly modified version
public int getTemp()
{
String t;
int temp;

System.out.println ("What is the current temperature in degrees?");
t = Reader.readLine();
if (t == "q") {
System.out.println("Program terminated!");
System.exit(0);
}
else
try {
temp = Integer.parseInt(t);
return temp;
}
catch (NumberFormatException e) {
System.out.println("Input error");
}
temp = 0;
return temp;
}

this should take-in the userinput and store it as the value (t), then comparj it to the value "q" and if it matches close down the system. If it doesen't match it will tray and convert it to an integer and the return that iteger as being the value of (temp). otherwise it will print a message saying "input error"
so tell me how close is this?
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so tell me how close is this?

Compile it. Write a test case. Fix the code as necessary (such as not using the == operator to compare String objects) until you get it working the way you want it to. Then, work on fixing the design.
[ July 12, 2005: Message edited by: Steve Morrow ]
 
Daniel Ryan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
duly noted. hey thanks for all the help guys
reply
    Bookmark Topic Watch Topic
  • New Topic