Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: I cannot figure out what the errors mean nor how to correct them. Please help.

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I cannot figure out what the errors mean nor how to correct them. Please help.

    Here is the input file "test.txt" is:

    5

    55 65 75 85 95

    Here is my code:

    import java.io.Console;
    import java.util.*;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
     
    public class Project{
       public static void main(String[] args)
            throws FileNotFoundException, IOException 
       {
     
            //Console console = System.console();
            System.out.println("----------------------------------------------------------------------\n");
     
            BufferedReader inputFile = new BufferedReader(
                new FileReader("test.txt")
            );
     
            while (inputFile.ready()) {
                String[] line = inputFile.readLine().split("\\s+");
                int length = Integer.parseInt(line[0]);
            }
     
            // Vectors for each letter grade  
            Vector A = new Vector(100);
            Vector B = new Vector(100);
            Vector C = new Vector(100);
            Vector D = new Vector(100);
            Vector F = new Vector(100);
     
     
            // For loop that repeats to program.
            for (int j = 1; j < (length + 1); j++) {
     
                //String a = console.readLine("What is your grade # %s ", j + 1);
                int NewGrade = Integer.parseInt(line[j]);
     
                NewGrade = line[j];
     
     
                if (NewGrade >= 90) {
                    A.addElement(new Integer(NewGrade));
                }
                else if (80 <= NewGrade && NewGrade <= 89) {
                    B.addElement(new Integer(NewGrade));
                } 
                else if (70 <= NewGrade && NewGrade <= 79) {
                    C.addElement(new Integer(NewGrade));
                } 
                else if (60 <= NewGrade && NewGrade <= 69) {
                    D.addElement(new Integer(NewGrade));
                } 
                else {
                    F.addElement(new Integer(NewGrade));
                }
     
            }
     
     
            System.out.println("Number of people who made an 'A': " + A.size());
            System.out.println("Number of people who made an 'B': " + B.size());
            System.out.println("Number of people who made an 'C': " + C.size());
            System.out.println("Number of people who made an 'D': " + D.size());
            System.out.println("Number of people who made an 'F': " + F.size());
     
     
            System.out.println("\n----------------------------------------------------------------------");
        }
    }



    And here are the error messages:


    C:\Users\Jared\Desktop\Project>javac Project.java
    Project.java:34: error: cannot find symbol
    for (int j = 1; j < (length + 1); j++) {
    ^
    symbol: variable length
    location: class Project
    Project.java:37: error: cannot find symbol
    int NewGrade = Integer.parseInt(line[j]);
    ^
    symbol: variable line
    location: class Project
    Project.java:39: error: cannot find symbol
    NewGrade = line[j];
    ^
    symbol: variable line
    location: class Project
    Note: Project.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    3 errors

    C:\Users\Jared\Desktop\Project>


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    Hello.
    You need to learn about scope of a variable in java.
    This link may be of help. Variable Scope

    Also, it shall help us if we know what exactly your program is doing. Based on that we can help you better.

    Syed.

  3. #3
    Member Hikaros's Avatar
    Join Date
    Sep 2013
    Posts
    42
    My Mood
    Love
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    for (int j = 1; j < (length + 1); j++)

    length of what?

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    Quote Originally Posted by syedbhai View Post
    Hello.
    You need to learn about scope of a variable in java.
    This link may be of help. Variable Scope

    Also, it shall help us if we know what exactly your program is doing. Based on that we can help you better.

    Syed.
    I apologize, I forgot to explain the program. Basically it's reading a list of grades from a file and putting a letter grade to them. It outputs the total number of each letter grade. The first number in the text file is the number of grades.[COLOR="Silver"]

    --- Update ---

    It's the length variable that was declared earlier in the code.

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    Quote Originally Posted by syedbhai View Post
    Hello.
    You need to learn about scope of a variable in java.
    This link may be of help. Variable Scope

    Also, it shall help us if we know what exactly your program is doing. Based on that we can help you better.

    Syed.
    Okay, here is my new code:

    import java.io.Console;
    import java.util.*;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
     
    public class Project{
       public static void main(String[] args)
            throws FileNotFoundException, IOException 
       {
            int length;
            String[] line;
            //Console console = System.console();
            System.out.println("----------------------------------------------------------------------\n");
     
            BufferedReader inputFile = new BufferedReader(
                new FileReader("test.txt")
            );
     
            while (inputFile.ready()) {
                line = inputFile.readLine().split("\\s+");
                length = Integer.parseInt(line[0]);
            }
     
            // Vectors for each letter grade  
            Vector A = new Vector(100);
            Vector B = new Vector(100);
            Vector C = new Vector(100);
            Vector D = new Vector(100);
            Vector F = new Vector(100);
     
     
            // For loop that repeats to program.
            for (int j = 1; j < (length + 1); j++) {
     
                //String a = console.readLine("What is your grade # %s ", j + 1);
                int NewGrade = Integer.parseInt(line[j]);
     
                //NewGrade = line[j];
     
     
                if (NewGrade >= 90) {
                    A.addElement(new Integer(NewGrade));
                }
                else if (80 <= NewGrade && NewGrade <= 89) {
                    B.addElement(new Integer(NewGrade));
                } 
                else if (70 <= NewGrade && NewGrade <= 79) {
                    C.addElement(new Integer(NewGrade));
                } 
                else if (60 <= NewGrade && NewGrade <= 69) {
                    D.addElement(new Integer(NewGrade));
                } 
                else {
                    F.addElement(new Integer(NewGrade));
                }
     
            }
     
     
            System.out.println("Number of people who made an 'A': " + A.size());
            System.out.println("Number of people who made an 'B': " + B.size());
            System.out.println("Number of people who made an 'C': " + C.size());
            System.out.println("Number of people who made an 'D': " + D.size());
            System.out.println("Number of people who made an 'F': " + F.size());
     
     
            System.out.println("\n----------------------------------------------------------------------");
        }
    }


    Here are my errors:


    C:\Users\Jared\Desktop\Project>javac Project.java
    Project.java:12: error: illegal start of expression
    protected int length;
    ^
    Project.java:13: error: illegal start of expression
    protected String[] line;
    ^
    2 errors

    C:\Users\Jared\Desktop\Project>

  6. #6
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    Did you post the right code?
    There is no "protected" keyword anywhere in your class. Just to be correct I compiled your source code in your most recent post.
    This is the compile-time error I get:
    Project.java:35: variable length might not have been initialized
            for (int j = 1; j < (length + 1); j++) {
                                 ^
    Project.java:38: variable line might not have been initialized
                int NewGrade = Integer.parseInt(line[j]);

    Syed.

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    I'm not sure what happened. I must have copied the wrong thing. The compile error that you got, was what I got.

    This:

    Project.java:35: variable length might not have been initialized
    for (int j = 1; j < (length + 1); j++) {
    ^
    Project.java:38: variable line might not have been initialized
    int NewGrade = Integer.parseInt(line[j]);

  8. #8
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    Those lines are very descriptive in the error itself. Both are initialized within the while loop but what happens if the condition in the while loop is not met? Those values will never be initialized and when you try to use them it won't know what to do. Best practice is to initialize the variables as soon as possible then modify them as you need to.

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    I initialized them before the while loop.

  10. #10
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    There is a difference between initializing a variable and creating a variable.

    // Create variables
    int firstInt; // This variable was just created not initialized
    int secInt = 100; // This variable was created an initialized
     
    firstInt = 500; // This variable has now been initialized

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    Another word for "created" that is used more commonly is Java is "declared."

  12. #12
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cannot figure out what the errors mean nor how to correct them. Please help.

    Thanks everyone for the help. I finished the program and got it working. Here is what I ended up with. If anyone has suggestions or tips, please let me know.

    My code:

    import java.io.Console;
    import java.util.*;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.*;
     
    public class Project{
       public static void main(String[] args)
            throws FileNotFoundException, IOException 
       {
            // Initializing Variables.
            int length = 0;
            String[] line = {};
     
            // Just for looks.
             System.out.println("\n" + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178
              + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178
               + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178
                + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + "\n");
     
            // Reading the number of grades and the actual grades from the file.
            BufferedReader inputFile = new BufferedReader(new FileReader("test.txt"));
            length = Integer.parseInt(inputFile.readLine());
            while (inputFile.ready()) {
                line = inputFile.readLine().split("\\s+");
            }
     
            // Vectors for each range of grades  .
            // [0-24]
            Vector A = new Vector(100);
            // [25-49]
            Vector B = new Vector(100);
            // [50-74]
            Vector C = new Vector(100);
            // [75-99]
            Vector D = new Vector(100);
            // [100-124]
            Vector E = new Vector(100);
            // [125-149]
            Vector F = new Vector(100);
            // [150-174]
            Vector G = new Vector(100);
            // [175-200]
            Vector H = new Vector(100);
            // This one if for any number that happens to not fit within a category.
            Vector I = new Vector(100);
     
     
            // For loop that repeats the program.                         
            for (int j = 0; j < length; j++) {
     
                // Initializing NewGrade Variable.
                int NewGrade = Integer.parseInt(line[j]);
     
                // Logic for categorizing the grades.
                if (0 <= NewGrade && NewGrade <= 24) {
                    A.addElement(new Integer(NewGrade));
                }
                else if (25 <= NewGrade && NewGrade <= 49) {
                    B.addElement(new Integer(NewGrade));
                } 
                else if (50 <= NewGrade && NewGrade <= 74) {
                    C.addElement(new Integer(NewGrade));
                } 
                else if (75 <= NewGrade && NewGrade <= 99) {
                    D.addElement(new Integer(NewGrade));
                } 
                else if (100 <= NewGrade && NewGrade <= 124) {
                    E.addElement(new Integer(NewGrade));
                }
                else if (125 <= NewGrade && NewGrade <= 149) {
                    F.addElement(new Integer(NewGrade));
                }
                else if (150 <= NewGrade && NewGrade <= 174) {
                    G.addElement(new Integer(NewGrade));
                }
                else if (175 <= NewGrade && NewGrade <= 200) {
                    H.addElement(new Integer(NewGrade));
                }
                else {
                    I.addElement(new Integer(NewGrade));
                }
     
            }
     
            // Printing the groups of grades out to the screen.
            System.out.println("[0 - 24]: " + A.size());
            System.out.println("[25 - 49]: " + B.size());
            System.out.println("[50 - 74]: " + C.size());
            System.out.println("[75 - 99]: " + D.size());
            System.out.println("[100 - 124]: " + E.size());
            System.out.println("[125 - 149]: " + F.size());
            System.out.println("[150 - 174]: " + G.size());
            System.out.println("[175 - 200]: " + H.size());
     
            // Just for looks.
            System.out.println("\n" + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178
                 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178
                  + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178
                   + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178 + (char)178);
        }
    }

Similar Threads

  1. Is this a correct use of synchronization?
    By Cornix in forum Threads
    Replies: 3
    Last Post: September 2nd, 2013, 06:21 PM
  2. is my code correct?
    By leonne in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 12th, 2012, 04:36 PM
  3. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  4. [SOLVED] No errors, but output not correct? Only one loop performed correctly! Help please :)
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2012, 11:18 AM
  5. I still have errors. Can you PLEASE correct my code?!
    By sam30317 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2011, 06:10 AM