Results 1 to 6 of 6
  1. #1
    AyuzawaM is offline Member
    Join Date
    Aug 2014
    Posts
    12
    Rep Power
    0

    Default Logical error with else if statement. Please help due in a few hours. >.>

    So I was almost finished with my program when I noticed I had a big logical error. At the very beginning of my else if statements I would jump down to my else statement. I have no clue why it is doing this. Please help. Please try to break things down for me as simple as possible, I haven't slept in over a day and I can't properly think right now. Anyways heres the code.

    Java Code:
    import java.util.Scanner;
    
    public class InternetServiceProviderNew
      {
        public static void main(String[] args)
    
         {
    
          double packageA, packageB, packageC, extraA, extraB, userHours, aHours, bHours, cHours, userCharges;
    
          packageA = 9.95; packageB = 13.95; packageC = 19.95; aHours = 10; bHours = 20;
    
          String userPackage, A, B, C ;
    
    
    
          Scanner keyboard = new Scanner(System.in);
    
             System.out.print("Enter the customer's package (A,B, or C) :");
             userPackage = keyboard.nextLine();
    
                if (userPackage == "A" ) {
                   System.out.println("Enter the number of hours used: ");
                   userHours = keyboard.nextDouble();
    
                   if (userHours >= 0 && userHours <= 720) {
    
                     extraA = (userHours - bHours);
    
                       if (extraA <= 0 ); {
                          System.out.println("The Charges are " + packageA);
    
                          if (extraA > 0) {
    
                          userCharges = packageA + (extraA * 2 );
    
                          }
    
                         }
                       }
    
    
    
    
    
                       if (userHours >= 0 && userHours <= 720)
                        {
                          System.out.println("The charges are ");
    
                        }
                   else if (userPackage == "B") {
                   System.out.println("Enter the number of hours used: ");
                   userHours = keyboard.nextDouble();
     }
    
                   else if (userPackage == "C") {
                                        }
    
    
    
          }
    
      }
    
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Logical error with else if statement. Please help due in a few hours. >.>

    Java beginner trap #1: do not compare objects such as String with ==, you compare them using the equals() method.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    AyuzawaM is offline Member
    Join Date
    Aug 2014
    Posts
    12
    Rep Power
    0

    Default Re: Logical error with else if statement. Please help due in a few hours. >.>

    Quote Originally Posted by gimbal2 View Post
    Java beginner trap #1: do not compare objects such as String with ==, you compare them using the equals() method.
    How would I do that? They haven't shown that to us in class.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Logical error with else if statement. Please help due in a few hours. >.>

    Well then I hope you have a good book to look it up in, or have access to Google.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Logical error with else if statement. Please help due in a few hours. >.>

    Java Code:
    <your string variable>.equals("thing to compare")
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Logical error with else if statement. Please help due in a few hours. >.>

    I already told you what your problem was in your other thread!

Similar Threads

  1. Irritating logical error, switch statement problem
    By Luftwaffle in forum New To Java
    Replies: 3
    Last Post: 05-20-2012, 11:42 PM
  2. Stuck on a Do While logical error
    By dougie1809 in forum New To Java
    Replies: 8
    Last Post: 01-23-2012, 06:05 PM
  3. Logical Operator error
    By MBD in forum New To Java
    Replies: 2
    Last Post: 10-11-2011, 01:29 PM
  4. Need help with an illogical logical statement
    By veloDMC in forum New To Java
    Replies: 3
    Last Post: 09-01-2011, 02:58 AM
  5. Replies: 3
    Last Post: 09-26-2010, 07:29 AM

Tags for this Thread

Posting Permissions

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