Results 1 to 9 of 9
  1. #1
    M.ubc is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Need Urgent Help with my Java Program import.java.util.Scanner; public class Ticke

    // This is my radar traffic program and i get 4 compile errors; 3 else without if errors and 1 identifier expected error on the first line. Please help me fix this. Thank you.




    import.java.util.Scanner; <--- identifier expected??
    public class Ticket{ // 1st opening
    public static void main(String[] args ) { // 2nd opening

    int base = 138;
    int middle = 250;
    int high = 450;
    int limit = 0;
    int priors = 0;
    int speed = 0;


    System.out.println( “Welcome to the Ticket Program Officer” );

    Scanner scanner = new Scanner(System.in);
    System.out.print( “Enter the speed limit “);
    limit = scanner.nextInt( );

    System.out.print( “Enter number of priors”);
    priors = scanner.nextInt( );


    Scanner stdin = new Scanner(System.in);
    String dblez = stdin.nextLine( );

    While(speed > 0); { // 3rd opening
    If( speed < limit) {
    System.out.println( “No Ticket “);
    }
    If( limit < speed <= limit + 19) ;{
    System.out.println( “Ticket is “ + base );
    }
    If ( limit + 20 < speed < = limit + 40 ); {
    System.out.println( “ Ticket is “ + middle);
    else
    System.out.println( “Ticket is “ + high);
    }

    If( dblez ==yes); {
    If ( limit < speed <= limit + 19)
    System.out.println( “Ticket is “ + base *2);

    If ( limit + 20 < speed < = limit + 40 );
    System.out.println( “ Ticket is “ + medium*2);
    else
    System.out.println( “Ticket is “ + high*2);
    }
    If( priors > 0); {

    If( limit < speed <= limit + 19);{
    System.out.println( “Ticket is “ + base *base); }

    If( limit + 20 < speed < = limit + 40 ); {
    System.out.println( “ Ticket is “ + medium*medium);
    else
    System.out.println( “Ticket is “ + high*high); }


    }


    } // 3rd closing
    } // 2nd closing
    } // 1st closing

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Need Urgent Help with my Java Program import.java.util.Scanner; public class T

    Please edit your post and wrap your code with code tags:
    [code]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Copy the full text of the error messages and paste it here.

    I recommend you use the -Xlint option with the javac command to get better compiler error messages:
    D:\Java\jdk1.7.0.7\bin\javac.exe -cp . -Xlint TestCode15.java


    One problem I see is the quote char surrounding a String are not both: ". Your keyboard must have different chars for starting and ending "s.
    Last edited by Norm; 10-16-2013 at 02:41 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Jamiemcg is offline Member
    Join Date
    Oct 2013
    Location
    Massachusetts
    Posts
    3
    Rep Power
    0

    Default Re: Need Urgent Help with my Java Program import.java.util.Scanner; public class T

    Hello, M.ubc!

    The reason that you're getting the "identifier expected" error on the first line when trying to compile your code is because there is a period between import and java (which is the beginning of the package and class that you're going to import).

    If you remove the period so that the line reads import java.util.Scanner;, that error will disappear.

    Regarding the other errors that are preventing you from compiling your program, there were semicolons included after the conditions of some of the if statements in your program, before the opening curly brace, which may have been causing some of the errors that you were receiving.

    Additionally, I would recommend that you post your code using code tags, so that it is displayed with proper syntax highlighting and formatting.

    I pasted your code into Dr. Java and edited it, (I had to change your quotation marks, since they were causing errors when I tried to compile, though I think that may be a minor issue and will probably not affect your code if you were able to compile without errors related to it previously) removing unnecessary semicolons and trying to format things appropriately.

    I also added a few curly braces, if I thought they may have been needed, though I could be mistaken due to it not being formatted properly when I copied and pasted it into Dr. Java.

    Java Code:
    import java.util.Scanner; //<--- identifier expected??
    public class Ticket{ // 1st opening
    public static void main(String[] args ) { // 2nd opening
    
    int base = 138;
    int middle = 250;
    int high = 450;
    int limit = 0;
    int priors = 0;
    int speed = 0;
    
    
    System.out.println( "Welcome to the Ticket Program Officer" );
    
    Scanner scanner = new Scanner(System.in);
    System.out.print( "Enter the speed limit");
    limit = scanner.nextInt( );
    
    System.out.print( "Enter number of priors"); 
    priors = scanner.nextInt( );
    
    
    Scanner stdin = new Scanner(System.in);
    String dblez = stdin.nextLine( );
    
    While(speed > 0) { // 3rd opening
      If( speed < limit) {
        System.out.println( "No Ticket ");
      }
      If( limit < speed <= limit + 19) {
        System.out.println( "Ticket is " + base );
      }
      If ( limit + 20 < speed < = limit + 40 ); {
        System.out.println( " Ticket is " + middle);
      }
      else {
          System.out.println( "Ticket is " + high);
    }
      
      If( dblez == "yes") {
        If ( limit < speed <= limit + 19)
          System.out.println( "Ticket is " + base *2);
        
        If ( limit + 20 < speed < = limit + 40 );
        System.out.println( " Ticket is " + medium*2);
        else
          System.out.println( "Ticket is " + high*2); 
      }
      If( priors > 0) {
        
        If( limit < speed <= limit + 19) {
          System.out.println( "Ticket is " + base *base); }
        
        If( limit + 20 < speed < = limit + 40 ) {
          System.out.println( " Ticket is " + medium*medium);
          else {
            System.out.println( "Ticket is " + high*high); 
          }
    
    
    }
    
    
    } // 3rd closing
    } // 2nd closing
    } // 1st closing
    While this code still didn't compile, hopefully some of the compiler errors that you were receiving are now gone.

    Best of luck and I hope I helped you!

    - Jamie

  4. #4
    M.ubc is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need Urgent Help with my Java Program import.java.util.Scanner; public class T

    Oh ok thanks guys. I tried your part Jamie and it did not work in getting rid of the else if errors, but also added more errors when i took out the semi colons, i was down to 3 errors and now at 12, all the added errors were missing semi colons, so im confused as to how what happened. Also i have no idea why the else without if keeps showing up.

    Java Code:
    public class Ticket{ // 1st opening
    public static void main(String[] args ) { // 2nd opening
    
    int base = 138;
    int middle = 250;
    int high = 450;
    int limit = 0;
    int priors = 0;
    int speed = 0;
    
    
    System.out.println( “Welcome to the Ticket Program Officer” );
    
    Scanner scanner = new Scanner(System.in);
    System.out.print( “Enter the speed limit “);
    limit = scanner.nextInt( );
    
    System.out.print( “Enter number of priors”); 
    priors = scanner.nextInt( );
    
    
    Scanner stdin = new Scanner(System.in);
    String dblez = stdin.nextLine( );
    
    While(speed > 0); { // 3rd opening
    If( speed < limit) {
    System.out.println( “No Ticket “);
    }
    If( limit < speed <= limit + 19) ;{
    System.out.println( “Ticket is “ + base );
    }
    If ( limit + 20 < speed < = limit + 40 ); {
    System.out.println( “ Ticket is “ + middle);
    else
    System.out.println( “Ticket is “ + high);
    }
    
    If( dblez ==yes); {
    If ( limit < speed <= limit + 19)
    System.out.println( “Ticket is “ + base *2);
    
    If ( limit + 20 < speed < = limit + 40 );
    System.out.println( “ Ticket is “ + medium*2);
    else
    System.out.println( “Ticket is “ + high*2); 
    }
    If( priors > 0); {
    
    If( limit < speed <= limit + 19);{
    System.out.println( “Ticket is “ + base *base); }
    
    If( limit + 20 < speed < = limit + 40 ); {
    System.out.println( “ Ticket is “ + medium*medium);
    else
    System.out.println( “Ticket is “ + high*high); }
    
    
    }
    
    
    } // 3rd closing
    } // 2nd closing
    } // 1st closing

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Need Urgent Help with my Java Program import.java.util.Scanner; public class T

    The code needs formatting. Nested statements should be indented 3-4 spaces to show the logic. The statements should not all start in the first column.

    Also you need to copy the full text of the error messages and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Need Urgent Help with my Java Program import.java.util.Scanner; public class T

    Don't capitalize keywords. Their case is part of their spelling.
    Get in the habit of using standard Java naming conventions!

  7. #7
    Jamiemcg is offline Member
    Join Date
    Oct 2013
    Location
    Massachusetts
    Posts
    3
    Rep Power
    0

    Default Re: Need Urgent Help with my Java Program import.java.util.Scanner; public class T

    Like Norm suggested, you should take a few minutes to go through your code and format it to show how the while loop and conditional statements are supposed to be nested, which would aid other members in helping you figure out what is causing the errors.

    Additionally, when nesting conditional statements, like if/else if/ else statements, you may want to use curly brackets and indentation to indicate which statements are nested within other statements, which will make the code more organized and easier to read.

    When using curly brackets, it is a good idea to ensure that every opening bracket has a closing bracket, and that you are consistent throughout your program. (You may want to include curly brackets, even if the conditional statement does not require them, so that it is consistent the rest of your code.)

    As for the semicolon issues, a semicolon is used to end a statement or method, and should not be used after a conditional statement, like an if statement, as seen in line 38 of the code you posted, which is If( dblez ==yes);. Removing the semicolon here (and after other conditional statements) will probably get rid of some of the errors that you are receiving when trying to compile your code.

    Another thing to note, also on line 38, is that if you're comparing the content of Strings, you should use the .equals() method, as using the "equality" operator (==) will check whether or not two Strings are in the same location in memory and not whether or not they are the same. (I actually made this error in my first reply to this thread, by wrapping quotation marks around "yes", and not using the .equals() method! My apologies for that!)

    So, instead of having:

    Java Code:
    If( dblez ==yes){
       //code here
    }
    or

    Java Code:
    If( dblez == "yes"){
       //code here
    }
    You should have:

    Java Code:
    If( dblez.equals("yes") ){
       //code here
    }
    Best of luck and I hope I helped you!

    - Jamie

  8. #8
    M.ubc is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need Urgent Help with my Java Program import.java.util.Scanner; public class T

    The program compiles now. It just stops at an infinite loop at the "I suck at programming" test string
    It asks what is speed limit, asks what are priors, then asks simultaneously if its doublezone and enter speed of vehicle? Then goes on infinite loop. Im not sure how to debug it.

    Java Code:
    import java.util.Scanner;
        public class TicketF{
        public static void main(String[] args){
    
            int base = 138;
            int medium = 290;
            int high = 450;
            int limit = 0;
            int priors = 0;
            int speed = 0;
    
            System.out.println("Welcome to the Ticket Program Officer");
    
            Scanner scanner = new Scanner(System.in);
            System.out.print(" Enter the speed limit");
            limit = scanner.nextInt();
    
            System.out.print("Enter number of priors");
            priors = scanner.nextInt();
    
            System.out.print(" Is it a doublezone");
            String dblez = scanner.nextLine();
    
            System.out.print(" Enter the speed of vehicle");
            speed = scanner.nextInt();
    
            while(speed > 0) {
                System.out.println(" I suck at programming");     // Infinite loop
                if(speed < limit) {
                    System.out.println("No Ticket");
            }
                if( speed >= limit + 40) {
                    System.out.println("Ticket is" + high);
                }
                if(limit + 20 <= speed){
                    System.out.println("Ticket is" + medium);}
                else{
                        System.out.println("Ticket is" + base);
    
                }
                if(dblez == "yes")
                    {
    
                        if(limit + 40 <= speed )
                            System.out.println("Ticket is" + high * 2);
    
                        if(limit + 20 <= speed)
                            System.out.println("Ticket is" + medium * 2);
                        else
                            System.out.println("Ticket is" + base * 2);
                    }
    
    
                if(priors > 0) {
                    if(limit + 40 <= speed)
                        System.out.println("Ticket is" + high * priors * priors);
                    else {
    
                        if(limit + 20 <= speed)
    
                            System.out.println("Ticket is" + medium * priors * priors);
                        else
                            System.out.println("Ticket is" + base * priors * priors);
                        }
                    }
    
                }
            }
        }

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Need Urgent Help with my Java Program import.java.util.Scanner; public class T

    Nowhere in that loop do you change the value of 'speed'. So if it runs once, of course it's going to loop forever. Maybe a 'while' loop is not what you want here.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Using import java.util.Scanner
    By The Taco in forum New To Java
    Replies: 2
    Last Post: 08-10-2013, 09:26 PM
  2. Replies: 3
    Last Post: 10-06-2009, 08:14 PM
  3. import java.util ????
    By kris09 in forum New To Java
    Replies: 3
    Last Post: 08-11-2008, 12:39 AM
  4. java.util.DateFormat not found in import
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 10:31 PM
  5. The import java.util.Scanner cannot be resolved
    By Heather in forum Advanced Java
    Replies: 1
    Last Post: 07-08-2007, 01:05 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
  •