Results 1 to 4 of 4
Thread: temperature program with inputs
- 10-16-2008, 06:37 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 2
- Rep Power
- 0
temperature program with inputs
This program asks you for your first and last name and then says hello, (first name, last name) and asks what the temperature is today. The temperature part is the problem i don't know how to do. I want it to ask for the temperature and if the number you give is greater than 72 to print a statement and if it is lower than 72 to print an alternate statement. Here is my code:
This is what the compiler says:Java Code:import java.io.*; public class Greetingtemp { public static void main(String args[]) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String firstName = ""; String lastName = ""; int w = Integer.parseInt(); while ("".equals(firstName)) { System.out.print("PLEASE ENTER YOUR FIRST NAME: "); firstName = in.readLine(); } while ("".equals(lastName)) { System.out.print("PLEASE ENTER YOUR LAST NAME: "); lastName = in.readLine(); } System.out.print("Hello " + firstName + " " + lastName); System.out.print(", what is the temperature(F) today? "); w = in.readLine(); if(w <= 72){ System.out.print("Maybe tomorrow will be nicer."); } else System.out.print("Why are you still at your computer?"); } }
Java Code:Z:\java\Greetingtemp.java:9: cannot find symbol symbol : method parseInt() location: class java.lang.Integer int w = Integer.parseInt(); ^ Z:\java\Greetingtemp.java:23: incompatible types found : java.lang.String required: int w = in.readLine(); ^ 2 errors
- 10-16-2008, 06:58 PM #2
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
I have pointed out the mistakes in the below program.Now you can understand easily
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String firstName = "";
String lastName = "";
String temp="";
while ("".equals(firstName)) {
System.out.print("PLEASE ENTER YOUR FIRST NAME: ");
firstName = in.readLine();
}
while ("".equals(lastName)) {
System.out.print("PLEASE ENTER YOUR LAST NAME: ");
lastName = in.readLine();
}
System.out.print("Hello " + firstName + " " + lastName);
System.out.print(", what is the temperature(F) today? ");
temp = in.readLine();
if(Integer.parseInt(temp)<= 72){
System.out.print("Maybe tomorrow will be nicer.");
}
else
System.out.print("Why are you still at your computer?");
- 10-17-2008, 06:31 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 2
- Rep Power
- 0
Thanks, it works now. Here is the final program:
Java Code:import java.io.*; public class Greetingtemp { public static void main(String args[]) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String firstName = ""; String lastName = ""; String temp=""; while ("".equals(firstName)) { System.out.print("PLEASE ENTER YOUR FIRST NAME: "); firstName = in.readLine(); } while ("".equals(lastName)) { System.out.print("PLEASE ENTER YOUR LAST NAME: "); lastName = in.readLine(); } System.out.print("Hello " + firstName + " " + lastName); System.out.print(", what is the temperature(F) today? "); temp = in.readLine(); if(Integer.parseInt(temp) <= 72){ System.out.print("Maybe tomorrow will be nicer."); } else System.out.print("Why are you still at your computer?"); } }
- 10-17-2008, 06:34 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In such a simple application why are you using BufferedReader? Scanner object is the best solution. There are huge different actually in those two classes.
Similar Threads
-
Temperature converstion
By jamesov89 in forum New To JavaReplies: 6Last Post: 09-29-2008, 04:51 AM -
Delay on inputs during calculation
By matt_well in forum New To JavaReplies: 14Last Post: 07-26-2008, 04:17 PM -
How to create this if many inputs?
By sarahannel123 in forum New To JavaReplies: 3Last Post: 05-18-2008, 04:22 PM -
Java program that stores user inputs
By staticy2003 in forum Advanced JavaReplies: 6Last Post: 01-24-2008, 07:46 PM -
Date Inputs
By hiranya in forum AWT / SwingReplies: 3Last Post: 11-06-2007, 05:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks