Results 1 to 3 of 3
  1. #1
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default why wouldn't it print my code?

    Java Code:
    import java.util.Scanner;
    
    public class InternetServiceProvider
    {
    	public static void main(String [] args)
    	{
    		Scanner keyboard = new Scanner (System.in);
    		
    		String letter;
    		double hours;
    		double total;
    		
    		
    		System.out.println("Package A: For $9.95 per month 10 hours of access are provided." +
    			" Additional hours\nare $2.00 per hour.\n");
    		System.out.println("Package B: For $13.95 per month 20 hours of access are provided." +
    			" Additional hours are\n$1.00 per hour.\n");
    		System.out.println("Package C: For $19.95 per month unlimited access is provided.\n");
    		System.out.print("Please enter a package you want(A, B, C): ");
    		letter = keyboard.nextLine();
    		System.out.print("Please enter hours used: ");
    		hours = keyboard.nextDouble();
    		
    		if (letter == "A" || letter == "A")
    		{
    			if(hours <= 10)
    			{
    				total = 9.95;
                System.out.println("You are using " + letter + " and you used " + hours +
    				" this month. Your total charge will be $" + total + ".");
    			}
    			else
    			{
    				total = 9.95 + ((hours-10)*2);
                System.out.println("You are using " + letter + " and you used " + hours +
    				" this month. Your total charge will be $" + total + ".");
    			}
    		}
    		else if(letter == "B" || letter == "B")
          {
             if(hours <= 20)
             {
                total = 13.95;
                System.out.println("You are using " + letter + " and you used " + hours +
    				" this month. Your total charge will be $" + total + ".");
             }
             else
             {
                total = 13.95 + ((hours-20)*1);
                System.out.println("You are using " + letter + " and you used " + hours +
    				" this month. Your total charge will be $" + total + ".");
             }
           }
          else if(letter == "C" || letter == "C")
          {
             total = 19.95;
             System.out.println("You are using " + letter + " and you used " + hours +
    				" this month. Your total charge will be $" + total + ".");
          }
        
    	}
    }
    As you can see, this problem, you have to make user to prompt internet service A, B, or C and number of hours used and print the total. When I compile, it compiles but it doesn't print the result at all. if I place println outside of brace, it tells me total is not initialized. What's wrong with my code and why wouldn't it print the result?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: why wouldn't it print my code?

    Probably the way you are comparing strings. Don't use ==, use equals().

    E.g. letter.equals("C")

    Also, I assume you want to compare both upper and lower case letters.
    But your target of comparisons are upper case. You can limit your comparison to one case by
    using one of the string methods of toUpperCase or toLowerCase.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

Similar Threads

  1. When would this code print out....
    By icykum in forum New To Java
    Replies: 3
    Last Post: 11-26-2012, 03:40 AM
  2. Replies: 1
    Last Post: 06-23-2011, 08:23 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
  •