Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Unknown Errors

  1. #1
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Unknown Errors

    Hi,
    So I am trying to code a program to calculate a phone bill based on what type of service the person has and how many minutes they used. I keep getting all kinds of errors, and when I fix a couple, more pop up. Can anyone tell me what I'm doing wrong and how to fix it please?
    Thanks
    Java Code:
    import java.util.Scanner;
    
    public class RobertSloanPhoneBill
    {
       public static void main(String [] args)
       {
          Scanner scan = new Scanner(System.in);
          
          int accnum = scan.nextInt();
          int code = scan.nextInt();
          int r;
          int p;
          double min;
          double dmin;
          double nmin;
          double total;
          double dtotal;
          double ntotal;
          
      
          System.out.println("Account number: " + accnum);
          System.out.println("Service Type: " + code);
          
          if (code == r)
          {
             min = scan.nextDouble();
             System.out.println("Number of mins: " + min);
             if (min <= 50)
                total = 10;
             else
                total = 10 + (min - 50)*.2;
          System.out.println("Amount due: " + total);
          }
          else if (code == p)
          {
             dmin = scan.nextDouble();
             nmin = scan.nextDouble();
             double ptotal = dmin + nmin;
             System.out.println("Number of mins: " + ptotal);
             if (dmin <= 75)
                total = 25;
             else
                dtotal = 25 + (dmin - 75) * .1;
             if (nmin <= 100)
                total = 25;
             else 
                ntotal = 25 + (nmin - 100) * .05;
          System.out.println("Amount due: " + ptotal);
          }     
       }         
    }
    Error Messages:

    Java Code:
     ----jGRASP exec: javac -g RobertSloanPhoneBill.java
    
    RobertSloanPhoneBill.java:30: error: variable r might not have been initialized
          if (code == r)
                      ^
    RobertSloanPhoneBill.java:40: error: variable p might not have been initialized
          else if (code == p)
                           ^
    2 errors
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Unknown Errors

    error: variable r might not have been initialized
    Assign r a value before using it.
    Same for p
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Unknown Errors

    Java Code:
     ----jGRASP exec: javac -g RobertSloanPhoneBill.java
    
    RobertSloanPhoneBill.java:21: error: cannot find symbol
          char r = regular;
                   ^
      symbol:   variable regular
      location: class RobertSloanPhoneBill
    RobertSloanPhoneBill.java:22: error: cannot find symbol
          char p = premium;
                   ^
      symbol:   variable premium
      location: class RobertSloanPhoneBill
    2 errors
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    This happens when I try to assign a char value. Same thing when it is int, except when I put a 0 for them. When I set the value of them to be zero, i get a run error, when I try to enter r or p for the service type.
    Java Code:
     ----jGRASP exec: java RobertSloanPhoneBill
    
    Enter account number: 123
    Enter service type: r
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Scanner.java:864)
    	at java.util.Scanner.next(Scanner.java:1485)
    	at java.util.Scanner.nextInt(Scanner.java:2117)
    	at java.util.Scanner.nextInt(Scanner.java:2076)
    	at RobertSloanPhoneBill.main(RobertSloanPhoneBill.java:19)
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Unknown Errors

    RobertSloanPhoneBill.java:21: error: cannot find symbol
    char r = regular;
    ^
    symbol: variable regular
    location: class RobertSloanPhoneBill
    RobertSloanPhoneBill.java:22: error: cannot find symbol
    char p = premium;
    ^
    symbol: variable premium
    location: class RobertSloanPhoneBill
    The compiler can not find definitions for the variables: regular and premium. Make sure those variables are defined and given values before trying to use them.

    Enter service type: r
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at RobertSloanPhoneBill.main(RobertSloanPhoneBill.jav a:19)
    The user must enter a number for the nextInt() method to read and convert. The letter r is not an number.
    Last edited by Norm; 09-13-2015 at 11:42 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Unknown Errors

    Ok so I need to set regular and premium up with a definition.... I need to be able to input r or p into the code and it to accept it, and read them for the if statements. How can I set up definitions for regular and premium? I set r = regular and p = premium, and I thought they didnt need anything else.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Unknown Errors

    How can I set up definitions for regular and premium?
    The same as any other variable. The syntax is:
    <the type> <the variable name> = <the initial value>;
    For example
    int xyz = 23;

    Note: There are Scanner methods to read in String values that can be assigned to String variables that can be tested later in the code in an if statement using the equals() method, for example.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Unknown Errors

    Ok so I am only trying to use r and p. It doesnt matter that regular and premium have values to this problem. I tried assigning regular and premium values, and the same error popped up.
    Java Code:
        char r = regular;
          char p = premium;
          char premium = p;
          char regular = r;
    Are you saying do something like String r = regular
    and use equals(r) in the if statement?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Unknown Errors

    You need to define a variable and give it a value BEFORE using it.
    Where are those two variables: regular and premium defined and given values?

    What type should regular be? char, String, int or ?
    What initial value should regular have? 'r', "R" or 123
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Unknown Errors

    Sorry if this sounds stupid, but did I not define it before I used it?
    Are they not defined and given values by char regular = r; ?

    I think it should be char because I need it to be "r"
    'r'

    Here is my modified code. I only need the variables r and p that I use in the if statements to work. I dont know if this helps or not.

    Java Code:
    import java.util.Scanner;
     
    public class RobertSloanPhoneBill
    {
       public static void main(String [] args)
       {
          Scanner scan = new Scanner(System.in);
          
          System.out.print("Enter account number: "); 
          int accnum = scan.nextInt();
          
          System.out.print("Enter service type: ");
          int code = scan.nextInt();
          
          char r = regular;
          char p = premium;
          char premium = p;
          char regular = r;
          
    
          double min;
          double dmin;
          double nmin;
          double total;
          double dtotal;
          double ntotal;
           
       
          System.out.println("Account number: " + accnum);
          System.out.println("Service Type: " + code);
           
          if (code == r)
          {
             min = scan.nextDouble();
             System.out.println("Number of mins: " + min);
             if (min <= 50)
                total = 10;
             else
                total = 10 + (min - 50)*.2;
          System.out.println("Amount due: " + total);
          }
          else if (code == p)
          {
             dmin = scan.nextDouble();
             nmin = scan.nextDouble();
             double ptotal = dmin + nmin;
             System.out.println("Number of mins: " + ptotal);
             if (dmin <= 75)
                total = 25;
             else
                dtotal = 25 + (dmin - 75) * .1;
             if (nmin <= 100)
                total = 25;
             else
                ntotal = 25 + (nmin - 100) * .05;
          System.out.println("Amount due: " + ptotal);
          }     
       }         
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Unknown Errors

    Are they not defined and given values by char regular = r; ?
    No. r is considered the name of a variable.
    If you want a char value wrap it in 's like 'r'
    If you want a String value, wrap it in "s like "r"
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Unknown Errors

    First, you have to declare the variable. E.g.

    int foo;

    Then you need to assign a legal value. E.g.

    foo = 23;

    You can do both at the same time:

    int foo = 23;

    But if you do this:

    int foo;
    int bar = foo; // foo doesn't contain anything, you get any error.

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

  12. #12
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Unknown Errors

    Ok, sorry for not understanding. I tried wrapping both, and both returned 6 errors...
    Java Code:
          char 'r' = regular;
          char 'p' = premium;
    Errors:
    Java Code:
     ----jGRASP exec: javac -g RobertSloanPhoneBill.java
    
    RobertSloanPhoneBill.java:21: error: not a statement
          char 'r' = regular;
          ^
    RobertSloanPhoneBill.java:21: error: ';' expected
          char 'r' = regular;
              ^
    RobertSloanPhoneBill.java:21: error: not a statement
          char 'r' = regular;
                     ^
    RobertSloanPhoneBill.java:22: error: not a statement
          char 'p' = premium;
          ^
    RobertSloanPhoneBill.java:22: error: ';' expected
          char 'p' = premium;
              ^
    RobertSloanPhoneBill.java:22: error: not a statement
          char 'p' = premium;
                     ^
    6 errors
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    Java Code:
          char r = 'regular';
          char p = 'premium';
    Errors:
    Java Code:
     ----jGRASP exec: javac -g RobertSloanPhoneBill.java
    
    RobertSloanPhoneBill.java:21: error: unclosed character literal
          char r = 'regular';
                   ^
    RobertSloanPhoneBill.java:21: error: unclosed character literal
          char r = 'regular';
                           ^
    RobertSloanPhoneBill.java:21: error: not a statement
          char r = 'regular';
                     ^
    RobertSloanPhoneBill.java:22: error: unclosed character literal
          char p = 'premium';
                   ^
    RobertSloanPhoneBill.java:22: error: unclosed character literal
          char p = 'premium';
                           ^
    RobertSloanPhoneBill.java:22: error: not a statement
          char p = 'premium';
                     ^
    6 errors
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    Last edited by MrLegitPancakes; 09-14-2015 at 01:25 AM.

  13. #13
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Unknown Errors

    Quote Originally Posted by jim829 View Post
    First, you have to declare the variable. E.g.

    int foo;

    Then you need to assign a legal value. E.g.

    foo = 23;

    You can do both at the same time:

    int foo = 23;

    But if you do this:

    int foo;
    int bar = foo; // foo doesn't contain anything, you get any error.

    Regards,
    Jim
    I understand that part, i'm just trying to define r and p. Why is it that they need to have a value before they can be used to determine which if statement to use? Or am I going about it in the wrong, and should use something else?

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Unknown Errors

    That's up to you. You could do:

    char regular = 'r';
    char apple ='a';

    String orange = "orange";

    int val = 23;

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

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Unknown Errors

    Another note re r: Single letter variable names make code hard to read and understand. It's better to use a variable name that describes what the variable contains.

    Take a look at the tutorial re datatypes: http://docs.oracle.com/javase/tutori...datatypes.html
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Unknown Errors

    Quote Originally Posted by jim829 View Post
    That's up to you. You could do:

    char regular = 'r';
    char apple ='a';

    String orange = "orange";

    int val = 23;

    Regards,
    Jim
    Ok, and is there a way to ask for user input for a char? I tried scan.nextChar() but that doesnt work. Is there another way?

  17. #17
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Unknown Errors

    Quote Originally Posted by Norm View Post
    Another note re r: Single letter variable names make code hard to read and understand. It's better to use a variable name that describes what the variable contains.

    Take a look at the tutorial re datatypes: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Thanks!

  18. #18
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Unknown Errors

    Quote Originally Posted by Norm View Post
    Another note re r: Single letter variable names make code hard to read and understand. It's better to use a variable name that describes what the variable contains.

    Take a look at the tutorial re datatypes: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Thanks!

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Unknown Errors

    Quote Originally Posted by MrLegitPancakes View Post
    Ok, and is there a way to ask for user input for a char? I tried scan.nextChar() but that doesnt work. Is there another way?
    There is no Scanner.nextChar() method. I suggest you use String variables and the next() method.

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

  20. #20
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Unknown Errors

    Ok so everything compiles now, but when I run it, it skips the enter service type line and finshes...
    Java Code:
    import java.util.Scanner;
     
    public class RobertSloanPhoneBill
    {
       public static void main(String [] args)
       {
          Scanner scan = new Scanner(System.in);
         
          System.out.print("Enter account number: "); 
          int accnum = scan.nextInt();
          
          System.out.print("Enter service type: ");
          String code = scan.nextLine();
          
          String regular = "r";
          String premium = "p";
    
          double min;
          double dmin;
          double nmin;
          double total;
          double dtotal;
          double ntotal;
           
          System.out.println("Account number: " + accnum);
          System.out.println("Service Type: " + code);
           
          if (code == "r")
          {
             min = scan.nextDouble();
             System.out.println("Number of mins: " + min);
             if (min <= 50)
                total = 10;
             else
                total = 10 + (min - 50)*.2;
          System.out.println("Amount due: " + total);
          }
          if (code == "p")
          {
             dmin = scan.nextDouble();
             nmin = scan.nextDouble();
             double ptotal = dmin + nmin;
             System.out.println("Number of mins: " + ptotal);
             if (dmin <= 75)
                ntotal = 25;
             else
                dtotal = 25 + (dmin - 75) * .1;
             if (nmin <= 100)
                ntotal = 25;
             else
                ntotal = 25 + (nmin - 100) * .05;
          System.out.println("Amount due: " + ntotal);
          }     
       }         
    }
    Run Output:
    Java Code:
     ----jGRASP exec: java RobertSloanPhoneBill
    
    Enter account number: 0123
    Enter service type: Account number: 123
    Service Type: 
    
     ----jGRASP: operation complete.
    Am I doing something wrong with the string and nextLine method?
    Last edited by MrLegitPancakes; 09-14-2015 at 03:11 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 15
    Last Post: 06-25-2012, 02:11 AM
  2. unknown error - really need help
    By smallmos1 in forum New To Java
    Replies: 2
    Last Post: 03-15-2011, 10:46 PM
  3. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  4. Unknown problem
    By sanchir0805 in forum New To Java
    Replies: 4
    Last Post: 03-20-2009, 03:56 AM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 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
  •