Results 1 to 4 of 4
  1. #1
    JingGong is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default 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:

    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?");
        
    		
    		}
       }
    This is what the compiler says:
    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

  2. #2
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    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?");

  3. #3
    JingGong is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

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

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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

  1. Temperature converstion
    By jamesov89 in forum New To Java
    Replies: 6
    Last Post: 09-29-2008, 04:51 AM
  2. Delay on inputs during calculation
    By matt_well in forum New To Java
    Replies: 14
    Last Post: 07-26-2008, 04:17 PM
  3. How to create this if many inputs?
    By sarahannel123 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 04:22 PM
  4. Java program that stores user inputs
    By staticy2003 in forum Advanced Java
    Replies: 6
    Last Post: 01-24-2008, 07:46 PM
  5. Date Inputs
    By hiranya in forum AWT / Swing
    Replies: 3
    Last Post: 11-06-2007, 05:11 PM

Posting Permissions

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