Results 1 to 5 of 5

Thread: If Statement

  1. #1
    aDrizzle is offline Member
    Join Date
    Jul 2007
    Location
    Lancaster, CA
    Posts
    4
    Rep Power
    0

    Default If Statement

    I am massively noobing at Java (and programming in general), and am getting very frustrated with it. I have a simple code typed up, and I'm sure my mistake is a simple fix, but there is nothing I can think to do to get my If statement to print.

    Java Code:
    import java.util.Scanner;
    
    public class Checkpoint3_2
    {
    	public static void main(String[] args)
    	{
    		Scanner keyIn = new Scanner(System.in);
    		
    		double rate;
    		int hours;
    		double overTime;
    		String name;
    		double pay;
    		
    		
    		pay = rate * hours;
    		overTime = rate * 1.5;
    		
    		
    		System.out.println("Hello.");
    		System.out.print("What is your name? ");
    		name = keyIn.nextLine();
    		
    		System.out.println("Hello " + name);
    		
    		System.out.print("Enter rate of pay. ");
    		rate = keyIn.nextDouble();
    		
    		System.out.print("Enter hours. ");
    		hours = keyIn.nextInt();
    		
    		if (hours > 40)
    		{
    			pay = overTime * (hours - 40) + (rate * 40);
    			System.out.println("You made " + pay);
    		}
    			else
    			{
    				System.out.println("You made " + pay);
    				System.out.println(":)");
    			}
    		
    	}
    }
    Last edited by JavaBean; 07-08-2007 at 01:04 PM. Reason: Code placed inside [code] tag.

  2. #2
    aDrizzle is offline Member
    Join Date
    Jul 2007
    Location
    Lancaster, CA
    Posts
    4
    Rep Power
    0

    Default

    Any help would be appreciated. Sorry, I didn't realize it would take away all my indents.

  3. #3
    aDrizzle is offline Member
    Join Date
    Jul 2007
    Location
    Lancaster, CA
    Posts
    4
    Rep Power
    0

    Default

    More specifically, and this just happened on a second program I just wrote:

    variable " " might not have been initialized

    I assume that may have something to do with it, though, I am unable to find anything on fixing it.

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    I assume that may have something to do with it, though, I am unable to find anything on fixing it.
    Yes, your mistake is simple and related to that warning message!

    Java Code:
    pay = rate * hours;
    overTime = rate * 1.5;
    The place of the above statements is incorrect. If you need to calculate something, you need to get inputs of that calculation first. But you do the calculation first and then get input! And compiler warns you because of that since you did not initialize rate and hours variables.

  5. #5
    aDrizzle is offline Member
    Join Date
    Jul 2007
    Location
    Lancaster, CA
    Posts
    4
    Rep Power
    0

    Default

    Arrghh, stupid mistake. I distinctly remember not remembering that in class lol. Thank you so much, uber life safer :)

Similar Threads

  1. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 04:55 PM
  2. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM
  3. goto statement
    By bugger in forum New To Java
    Replies: 2
    Last Post: 12-12-2007, 03:47 PM
  4. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:53 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 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
  •