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

    Default Problem with if-else statement... due tomorrow :(

    So I've been writing a program for my class and I can't seem to figure out how to fix my error. If someone could tell me how to fix this mistake I would appreciate it.






    Here's the program


    import java.util.Scanner;

    public class InternetServiceProviderPart1
    {
    public static void main(String[] args)

    {

    double packageA, packageB, packageC, extraA, extraB, userHours, userExtra;

    char userPackage;

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the customer's package (A,B, or C) :");
    userPackage = keyboard.nextChar();

    if (userPackage == A) {
    System.out.println("Enter the number of hours used:");
    }



    }

    }






    And this is the error I get when I try to compile.






    InternetServiceProviderPart1.java:22: cannot find symbol
    symbol : method nextChar()
    location: class java.util.Scanner
    userPackage = keyboard.nextChar();
    ^
    InternetServiceProviderPart1.java:24: cannot find symbol
    symbol : variable A
    location: class InternetServiceProviderPart1
    if (userPackage == A) {
    ^
    2 errors








    Please and thanks in advance :)

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Problem with if-else statement... due tomorrow :(

    Try formatting your code, that might help you solve the problem. You should also put your code in [ code] [/code] tags so that it is actually readable.

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

    Default Re: Problem with if-else statement... due tomorrow :(

    The error messages are self explanatory.
    There is no method called nextChar in the Scanner class.
    You have not declared a variable called A, perhaps you meant the char A instead.

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

    Default Re: Problem with if-else statement... due tomorrow :(

    Ah. So I should use nextLine?

    Also an update when I tried to get help elsewhere, got it down to one error now.

    [ code]
    InternetServiceProviderPart1.java:17: cannot find symbol
    symbol : class string
    location: class InternetServiceProviderPart1
    string userPackage, A, B, C ;
    ^
    1 error
    [/code]

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

    Default Re: Problem with if-else statement... due tomorrow :(

    Please don't multipost. People do not like wasting their time.

    BTW read the friggin error message!!!!!

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

    Default Re: Problem with if-else statement... due tomorrow :(

    sorry for not knowing how to use tags, i just registered when i posted this




    import java.util.Scanner;

    public class InternetServiceProviderPart1
    {
    public static void main(String[] args)

    {

    double packageA, packageB, packageC, extraA, extraB, userHours, userExtra;

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



    }

    }

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

    Default Re: Problem with if-else statement... due tomorrow :(

    Quote Originally Posted by Junky View Post
    Please don't multipost. People do not like wasting their time.

    BTW read the friggin error message!!!!!
    Ah sorry I'm really knew to this.

    I did but I'm a complete idiot. I'm brainfarting hard right now due to my lack of experience with programming.

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

    Default Re: Problem with if-else statement... due tomorrow :(

    Get in the habit of reading error messages. Java provides some informative messages better than most languages. It tells you what is wrong and on what line it occurs. Even if you cannot understand the message go to the line number indicated (or sometimes the line above) and closely examine it to see what could possibly be wrong. You can even place some debugging statements before the error occurs. Simply use print statements to display values of variables to ensure they hold what you think they do.

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

    Default Re: Problem with if-else statement... due tomorrow :(

    Quote Originally Posted by Junky View Post
    Get in the habit of reading error messages. Java provides some informative messages better than most languages. It tells you what is wrong and on what line it occurs. Even if you cannot understand the message go to the line number indicated (or sometimes the line above) and closely examine it to see what could possibly be wrong. You can even place some debugging statements before the error occurs. Simply use print statements to display values of variables to ensure they hold what you think they do.
    Just figured out I didn't declare the string with a capital S at the beginning after staring at it for about an hour. I'm going to keep this thread updated in case I do something stupid like that again..

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

    Default Re: Problem with if-else statement... due tomorrow :(

    Quote Originally Posted by Junky View Post
    Get in the habit of reading error messages. Java provides some informative messages better than most languages. It tells you what is wrong and on what line it occurs. Even if you cannot understand the message go to the line number indicated (or sometimes the line above) and closely examine it to see what could possibly be wrong. You can even place some debugging statements before the error occurs. Simply use print statements to display values of variables to ensure they hold what you think they do.
    debugging statements?

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

    Default Re: Problem with if-else statement... due tomorrow :(

    Updated.

    import java.util.Scanner;

    public class InternetServiceProviderPart1
    {
    public static void main(String[] args)

    {

    double packageA, packageB, packageC, extraA, extraB, userHours, userExtra;

    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:");
    }
    else (userPackage == "B") {
    };
    else (userPackage == "C") {
    };



    }

    }


    How would I make it so that at the else statement it if A, B, or C are not inputed I would send an error stating there is an invalid package?(my assignment is about service packages)

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

    Default Re: Problem with if-else statement... due tomorrow :(

    If statements can only have one else branch but hey can have as many else if branches as you like.
    Comparing Strings with == does not always give correct results. Use the equals method instead.
    Also the semi-colon after your braces are unnecessary.

Similar Threads

  1. Replies: 7
    Last Post: 02-11-2013, 05:28 PM
  2. Need help ASAP! Problem due tomorrow!
    By ProtoMan in forum New To Java
    Replies: 19
    Last Post: 02-22-2011, 06:00 AM
  3. Replies: 16
    Last Post: 06-27-2010, 05:16 AM
  4. Ordered Array plz help....due tomorrow!!
    By canyonist in forum New To Java
    Replies: 6
    Last Post: 05-23-2010, 10:57 AM
  5. Need help~~~ Midterm tomorrow!!!
    By str4kt in forum New To Java
    Replies: 23
    Last Post: 10-10-2008, 10:12 AM

Posting Permissions

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