Results 1 to 9 of 9
  1. #1
    Morgan is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Problem compiling with BlueJ

    Hello, I am trying to brush up on Java (haven't done any programing in a while), and was working through a tutorial. I kept getting an error message when I try to compile a piece of program that says "<identifier> expected" I thought maybe I was typing it wrong, so finally copied and pasted the program from the tutorial, tried to compile, and got the same message. I am using BlueJ version 3.0.9. Here is a copy of the text I am trying to compile:

    public class This
    {
    Scanner kbdIn = new Scanner(System.in); // Instantiating a new Scanner object
    System.out.print("Enter your name: "); // Printing out the prompt
    String name = kbdIn.nextLine(); // Reading a line of input (until the user hits enter) from the keyboard
    // and putting it in a String variable called name
    System.out.println("Welcome, " + name); // Printing out welcome, followed by the user's name
    }

    The line BlueJ highlights when it gives this message is:
    System.out.print("Enter your name: ");
    and it puts a red cursor between print and ( like it is expecting something between the two. Thank you for any help.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,779
    Rep Power
    5

    Default Re: Problem compiling with BlueJ

    You need to put your code inside main. And I would highly recommend calling your class something other than This. This with a lower case t is a keyword and although there is nothing wrong with your class it could be confusing.

    Java Code:
    public class Example {
       public static void main(String [] args) {
     
    // the rest of your code here
    
    
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Morgan is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Re: Problem compiling with BlueJ

    Jim,
    Thank you for the advise, but do you have any idea about the problem I am having compiling? I don't understand what BlueJ could be looking for there.

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

    Default Re: Problem compiling with BlueJ

    Have you moved your code as Jim suggests?
    If so can you post the corrected code and any new errors you are getting?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Morgan is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Re: Problem compiling with BlueJ

    Ok, here is the updated code, using Jim's suggestions:

    Java Code:
    public class Example
    {
        public static void main(String [] args){
         Scanner kbdIn = new Scanner(System.in); // Instantiating a new Scanner object
    System.out.print( "Enter your name: "); // Printing out the prompt
    String name = kbdIn.nextLine(); // Reading a line of input (until the user hits enter) from the keyboard
    // and putting it in a String variable called name
    System.out.println("Welcome, " + name); // Printing out welcome, followed by the user's name
        }
        
    }
    The error now says "cannot find symbol - class Scanner"

    also, this is copied and pasted from the class, this is everything minus the info at the top (date, name, etc.)

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,779
    Rep Power
    5

    Default Re: Problem compiling with BlueJ

    You probably need an import statement so try putting this at the beginning of your .java file.

    import java.util.Scanner;

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

  7. #7
    Morgan is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Re: Problem compiling with BlueJ

    Thank you for the help, but I have another question. I am writing a program to run some repetitive equations for my own use. I have gotten it to take the inputs, and the math works out correctly, as long as I only use whole numbers. If I try to input a decimal (which is pretty much every I would use the formulas), the program crashes and gives an error. The line it is giving the error on is:

    double w = scan.nextInt();

    and the error message says:

    java.util.InputMismatchException: null (in java.util.Scanner)

    Like I said before, the program works fine if all the user inputs are whole numbers (there is 4 inputs at this time, with more to be added later)
    Thanks in advance.

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,779
    Rep Power
    5

    Default Re: Problem compiling with BlueJ

    Check out the scanner documentation. But for this example, use

    Java Code:
    double w = scan.nextDouble();
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Morgan is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Re: Problem compiling with BlueJ

    Ok, Jim. That worked, program seems to be working fine now. Input 5 variables, runs the calculations and outputs all 11 variables without crashing. Ran it several times to make sure it was working properly. Thank you so much for all the help, this will save me a bunch of time with a calculator.

Similar Threads

  1. Replies: 1
    Last Post: 01-11-2013, 10:28 PM
  2. Bar chart Bluej problem
    By Manny123 in forum New To Java
    Replies: 1
    Last Post: 10-24-2012, 11:43 PM
  3. Error Compiling Project in BlueJ
    By java1337 in forum New To Java
    Replies: 9
    Last Post: 06-17-2011, 08:04 PM
  4. Is the problem with my code or bluej?
    By farahm in forum Advanced Java
    Replies: 2
    Last Post: 03-26-2011, 03:00 PM
  5. bluej hashset problem ;s
    By truant420 in forum Other IDEs
    Replies: 2
    Last Post: 12-07-2010, 05:56 PM

Posting Permissions

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