Why is the compiler complaining about an invalid expression run error message?

This code looks through multiple files to find the popularity and meaning of whatever name you enter. The class constants that have errors refer to the graph, which I still have to do in the class. They are not being used right now, but soon.

I am getting the following errors:

BabyNames.java:8: error: illegal start of expression
   public static final int STARTINGYEAR = 1890;
   ^
BabyNames.java:8: error: illegal start of expression
   public static final int STARTINGYEAR = 1890;
          ^
BabyNames.java:8: error: ';' expected
   public static final int STARTINGYEAR = 1890;
                ^
BabyNames.java:9: error: illegal start of expression
   public static final int WIDTH = 60;
   ^
BabyNames.java:9: error: illegal start of expression
   public static final int WIDTH = 60;
          ^ 
BabyNames.java:9: error: ';' expected
   public static final int WIDTH = 60;
                ^
BabyNames.java:10: error: illegal start of expression
   public static final int HEIGHT = 30; 
   ^
BabyNames.java:10: error: illegal start of expression
   public static final int HEIGHT = 30; 
          ^
BabyNames.java:10: error: ';' expected
   public static final int HEIGHT = 30;

      

I think I have all the correct semi-columns, but I'm not sure what's going on Here is my code:

import java.util.*;
import java.io.*;
//import java.lang.*;

public class BabyNames{ //ADD COMMENTS
   public static void main(String[] args) throws FileNotFoundException{
   public static final int STARTINGYEAR = 1890;
   public static final int WIDTH = 60;
   public static final int HEIGHT = 30; 
      Scanner console = new Scanner(System.in);
      DrawingPanel panel = new DrawingPanel(780,560);
      Graphics g = panel.getGraphics();
      Scanner nameFile = new Scanner(new File("names.txt"));
      Scanner meaningsFile = new Scanner(new File("meanings.txt"));
      Scanner nameFile2 = new Scanner(new File("names2.txt"));
      intro();
      //nameToLowerCase(console);
      if(STARTINGYEAR = 1890){
      findingStatistics(console,nameFile);
      }
      else{
      findingStatistics(console, nameFile2);       
      drawGraph(g);
   }



   public static void intro(){
      System.out.println("This program allows you to search through the");
      System.out.println("data from the Social Security Administration");
      System.out.println("to see how popular a particular name has been");
      System.out.println("since 1890" );
      System.out.println();
      System.out.print("Name: ");
      }

   public static String nameToLowerCase(Scanner console, Scanner data){   
      String originalName = console.next();
      String name = "" ;
      int lengthOfName = originalName.length();
      String beginingOfName = originalName.substring(0,1).toUpperCase();
      String endOfName = originalName.substring(1,lengthOfName).toLowerCase();
      name = beginingOfName + endOfName;
      return name;
   }
   public static String findingStatistics(Scanner console, Scanner data){
      String nameFinal = nameToLowerCase(console);
      boolean goesThroughOnce = false; //
      String statistics = "";
      String currWord = "";
      String currLine = "";
      while (data.hasNext() && goesThroughOnce == false){ 
         currLine = data.nextLine();
         Scanner lineBeingRead = new Scanner(currLine); //make other scanners?? for each file
         currWord = lineBeingRead.next(); //

         if (currWord.equals(nameFinal) || currWord.equals(nameFinal.toUpperCase())){   //         
            statistics = currLine;
            goesThroughOnce = true;
            System.out.println(statistics);
        }
        else{
        }
   }
   return statistics;
}

      

+3


source to share


3 answers


They are called constants in Java and they must be defined in the class itself, not in its main method.

public static final int STARTINGYEAR = 1890;
..... 

      



Now if you delete public static

you can keep them in your main method, but it doesn't seem to be what you intended. It also makes it impossible to use them in other methods, as they will become local final variables of the main method.

+3


source


There are several errors:

  • move static elements from main method

  • DrawingPanel needs to be imported (if not in one package)

  • Or change the parameters

    nameToLowerCase (scanner console, scanner data)

to

nameToLowerCase(Scanner console)

      



or add the seconds parameter to its call

String nameFinal = nameToLowerCase(console);

      

To avoid such errors, I recommend using an ideal like eclipse or intellij.

+1


source


Move your static elements outside of the main method:

public class BabyNames
{
   public static final int STARTINGYEAR = 1890;
   public static final int WIDTH = 60;
   public static final int HEIGHT = 30; 

   public static void main(String[] args) throws FileNotFoundException
   {
       Scanner console = new Scanner(System.in);
       DrawingPanel panel = new DrawingPanel(780,560);
       Graphics g = panel.getGraphics();
       Scanner nameFile = new Scanner(new File("names.txt"));
       Scanner meaningsFile = new Scanner(new File("meanings.txt"));
       Scanner nameFile2 = new Scanner(new File("names2.txt"));
       intro();
       //nameToLowerCase(console);
       if(STARTINGYEAR = 1890){
           findingStatistics(console,nameFile);
       } else {
           findingStatistics(console, nameFile2);       
           drawGraph(g);
       }
   }

      

And indent your code to help you find missing curly braces (like at the end of your main method).

0


source







All Articles