Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: why wouldn't it print my code?

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default why wouldn't it print my 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
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

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

    Give an example of your input and what does it output

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

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

    Do not use == to compare Strings or other objects. Use the equals method instead.

    --- Update ---

    BTW both clauses in your if statements are the same (capital A or capital A)
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    when i use "=", it says i need boolean. how do I put boolean here?

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

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

    Read my above post. ALL of it.
    Improving the world one idiot at a time!

Similar Threads

  1. Print just what I want, help me with my code.
    By JosPhantasmE in forum What's Wrong With My Code?
    Replies: 44
    Last Post: January 22nd, 2013, 07:44 PM
  2. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  3. Wordguess program wouldn't run
    By JerryRong in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2011, 12:18 AM
  4. [SOLVED] The button wouldn't show on the panel
    By nggdowt in forum AWT / Java Swing
    Replies: 7
    Last Post: November 3rd, 2011, 09:31 PM
  5. letterGrade not print out on my code
    By troj4nk1ng in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 8th, 2011, 08:31 PM