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 24 of 24

Thread: Syntax error!!!

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Syntax error!!!

    I'm finishing this demo using get(whatever) in order to test a class
     
    /**
     * The  ReportGenerator class uses the StudentReport1 class to simulate the report 
     * of a student. Holds fields for number of classes,grades received, average, and gpa.
     * 
     * @author Luis Fierro
     * @version 4/16/2014
     */
    public class ReportGenerator
    {
        public static void main(String [] args)
      {
        // declare variables
        private int numOfClasses;       //number of classes of the student
        private double grades;          //grades received in each class
        private average;                //average= sum of all grades / number of classes
        private gpa;                    //average converted to scale 1-4
        double total=0;			        //total of the grades 
    	String trash;			        //to convert string to double
     
     
          // Get the number of classes of the student
          numOfClasses = getNumOfClasses();
     
          // Get the grades of the student
          grades = getGrades();
     
          //Get total
          tota= getTotal(grades);
     
          // Get the average
          average = getAverage(numOfGrades, total);
     
          // Get the gpa
          gpa = getGpa(average);
     
          // Display the Data
          displayData(average, gpa);
     
          System.exit(0);
     
      }
     
     
       public static double getNumOfClasses()
       {
          String input;        // To hold keyboard input
     
          input = JOptionPane.showInputDialog(
                     "Number of classes: ");
          return Double.parseDouble(input);
     
          for (int i = 1; i <=numOfGrades; i++) 
    				{
     
    			trash=JOptionPane.showInputDialog("Test " + i + " : " );
    			grades=Integer.parseInt(trash);
     
     
    				total+=grades;	
     
     
     
     
       public static double getGrades()
       {
          String input;        // To hold keyboard input
     
          input = JOptionPane.showInputDialog(
                         "Grades received: ");
          return Double.parseDouble(input);
       }
     
        public static double getTotal(double grades)
       {
     
          return =+grades;
       }
     
     
     
       public static double getAverage(double total, double numOfClasses)
       {
          return total / numOfClasses;
     
       }
     
       public static double getGpa(double average)
       {
          return (average*4)/100;
     
       }
     
     
     
       public static void displayData(double average, double gpa,
                                      double area)
       {
          JOptionPane.showMessageDialog(null, "Name :" + getName() + "\nGrade" + getSGrade() +
                                        "\nSchool: " + getSName + "\nAverage: " + average + 
                                        "n\GPA: " + gpa + ".");
       }
      }
    }

    This is my class to test:
     
    /**
     * The StudentReport1 class has fields for name, grade, school, of a student
     * 
     * @author Luis Fierro
     * @version 4/05/2014
     */
    public class StudentReport1
    {
        // instance variables - replace the example below with your own
        private String name;
        private String sGrade;
        private String sName;
     
        /**
         * Constructor for objects of class StudentReport1
         * @param n The student's name
         * @param sg The student's grade in school
         * @param sn The student's school name
         * 
         */
        public StudentReport1(String n, String sg, String sn)
        {
            // initialise instance variables
            name = n;
            sGrade = sg;
            sName = sn;
        }
     
        /**
         * setName method 
         * 
         * @param  n The student's name
         */
        public void setName(String n)
        {
            name = n;
        }
     
        /**
         * setSGrade method 
         * 
         * @param  sg The student's grade in school
         */
        public void setSGrade(String sg)
        {
            sGrade = sg;
        }
     
        /**
         * setSName method 
         * 
         * @param  sn The student's school name
         */
        public void setSName(String sn)
        {
            sName = sn;
        }
     
        /**
         * getName method 
         * @return n Return the name of the student 
         */
        public String getName()
        {
            return name;
        }
     
        /**
         * getSGrade method
         * @return sg Return the student's grade in school
         * 
         */
        public String getSGrade()
        {
            return sGrade;
        }
     
        /**
         * getSName method
         * @return sn Return the student's school name
         */
        public String getSName()
        {
            return sName;
        }
     
        public String toString()
        {
            return "Name: " + name + "\nGrade: " + sGrade + "\nSchool"
                    + sName;
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    Did you have a question?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Gerock7 (April 22nd, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    I haven't change the double numOfClasses sorry, but the main problem is that it says error and I don't know what is wrong in my code
    I Think there must be something wrong with the getNumOfClasses part and getTotal
    but I don't knwo what is wron

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    it says error and I don't know what is wrong in my code
    Please copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Gerock7 (April 22nd, 2014)

  7. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    I change the variable to :
    private int numOfClasses;
    but it says illegal start of expression

    --- Update ---

    haha sorry thats was a dumb question it was because I declared the variables inside the main method instead of outside
    I also missed the double part of average and gpa
    But now I'm getting:
    Non-static variable numOfGrades cannot be referenced from a static context

    --- Update ---

    The code should dispaly The information of the user and the average, and gpa but it is not working in that way. When I fix something there are more errors and Idk what to do

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    Non-static variable numOfGrades cannot be referenced from a static context
    Either make numOfGrades static
    or create an instance of the class and use the reference to that instance to call its method.

    there are more errors and Idk what to do
    copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Gerock7 (April 22nd, 2014)

  10. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    It doesn't really say error it just crash when running and Idk what I suppose to fix, idk if you could look the code of the demo. Thats where the error should be

    --- Update ---

     
    /**
     * The  ReportGenerator class uses the StudentReport1 class to simulate the report 
     * of a student. Holds fields for number of classes,grades received, average, and gpa.
     * 
     * @author Luis Fierro
     * @version 4/16/2014
     */
    public class ReportGenerator
    {
     
        public static void main(String [] args)
      {
               // declare variables
        int numOfGrades;       //number of classes of the student
        int grades;          //grades received in each class
        double average;                //average= sum of all grades / number of classes
        double gpa;                    //average converted to scale 1-4
        int total=0;			        //total of the grades 
    	String trash;			        //to convert string to double
     
     
     
     
          // Get the number of classes of the student
          numOfGrades =  getNumOfGrades();
     
     
     
          //Get total
          total= getTotal(grades);
     
          // Get the average
          average = getAverage(numOfGrades, total);
     
          // Get the gpa
          gpa = getGpa(average);
     
          // Display the Data
          displayData(average, gpa);
     
          System.exit(0);
     
      }
     
     
       public static int getNumOfGrades()
       {
          String input;        // To hold keyboard input
     
          input = JOptionPane.showInputDialog(
                     "Number of classes: ");
          return Double.parseDouble(input);
     
      }
     
     
       public static double getGrades(int numOfGrades)
       {
          String input;        // To hold keyboard input
     
           //to get total  
          for (int i = 1; i <=numOfGrades; i++) 
    				{
     
    			trash=JOptionPane.showInputDialog("Test " + i + " : " );
    			grades=Integer.parseInt(trash);
     
     
     
    				total+=grades;	
                    }
     
       }
     
        public static double getTotal(double grades)
       {
     
          return total=+grades;
       }
     
     
     
       public static double getAverage(double total, int numOfClasses)
       {
          return total / numOfClasses;
     
       }
     
       public static double getGpa(double average)
       {
          return (average*4)/100;
     
       }
     
     
     
       public static void displayData(double average, double gpa,
                                      double area)
       {
          JOptionPane.showMessageDialog(null, "Name :" + StudentReport.getName() + "\nGrade" 
                                        + StudentReport.getSGrade() + "\nSchool: "
                                        + Student.getSName
                                        + "\nAverage: " + average + 
                                        "\nGPA: " + gpa + ".");
       }
      }
    I made some changes but still doesn't run

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    it just crash when running
    There must be an error message printed on the console somewhere that you need to copy the full text of the error message and paste it here so we can see it.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    there's something
    possible loss of precision
    required: int; founr: double

    total=getTotal(grades);

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    required: int; founr: double

    total=getTotal(grades);
    You need to copy the error message exactly so it is correct and complete. What you posted has a misspelling: founr that shows you typed it instead of copying it. You left off the ^ which is underneath where the compiler found the error.

    The method takes a double as an argument, but the code is passing it an int. Change one or the other so that they are the same.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Gerock7 (April 22nd, 2014)

  15. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    I changed my code and declared
    StudentReport1 method = new StudentReport1();

    but is says "constructor StudentReport1 in class StudentReport1 cannot be applied
    to given types; required: java.lang.String,java.lang.String,java.lang.String
    found:no arguments
    reason: actual and formal argument lists differ in length

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    actual and formal argument lists differ in length
    A call to a constructor MUST use the exact same number and type of arguments that are shown in the definition. There can be more than one constructor, but the call to the constructor must match one of them.

    Either add a constructor that takes the number of args you want to pass to the constructor
    or add the correct args to the call to the constructor that the code is trying to make.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #13
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    I have this until now but how can I do in order to name a method inside the main method like
    I have public static void main(String[]args)
    {
    public main StudentReport1 getData()
    {
    I know that is wrong but how do I write it without affecting the rest or isolating the rest

    import javax.swing.*;
    /**
     * The  ReportGenerator class uses the StudentReport1 class to simulate the report 
     * of a student. Holds fields for number of classes,grades received, average, and gpa.
     * 
     * @author Luis Fierro
     * @version 4/16/2014
     */
    public class ReportGenerator
    {
     
        public static void main(String [] args)
      {
     
          // declare variables
        int numOfGrades;       //number of classes of the student
        int grades;          //grades received in each class
        double average;                //average= sum of all grades / number of classes
        double gpa;                    //average converted to scale 1-4
        int total=0;			        //total of the grades 
    	String trash;			        //to convert string to double
     
     
            /**
         * getData method creates a StudentReport1 object
         * pobulated with data from the user
         * 
         * @return A reference to StudentReport1
         */
         public main StudentReport1 getData()
        {
            //variables to hold  the information
             String Name;
             String schoolGrade;
             String schoolName;
     
            //read the information
            name=JOptionPane.showInputDialog("Enter your name: ");
            schoolGrade=JOptionPane.showInputDialog("Enter your grade in school: ");
            schoolName=JOptionPane.showInputDialog("Enter the name of your school: ");
     
            //create a phoneBook entry object
            StudentReport1 data = new StudentReport1(name, schoolGrade, schoolName);
     
            //return a reference to the object
            return data;
     
        }
     
          // Get the average
          average = getAverage(numOfGrades, total);
     
          // Get the gpa
          gpa = getGpa(average);
     
     
          //ask number of grades
          trash=JOptionPane.showInputDialog("Number of grades:");             
    		numOfGrades=Integer.parseInt(trash);				              		                                                                  // All in one
     
    	  //get the grades added together in order to calculate the average
    		for (int i = 1; i <=numOfGrades; i++) 
    				{
     
    			trash=JOptionPane.showInputDialog("Test " + i + " : " );
    			grades=Integer.parseInt(trash);
     
     
    				total+=grades;	
     
    				}
      }
     
       public static double getAverage(double total, int numOfClasses)
       {
          return total / numOfClasses;
     
       }
     
       public static double getGpa(double average)
       {
          return (average*4)/100;
     
       }
     
     
       /**
         * showData method shows the data stored in  
         * the  StudentReport1 object
         * 
         * @param data The data to show 
         */
     
       public static void showData(StudentReport1 data, double average, double gpa,
                                      double area)
       {
          JOptionPane.showMessageDialog(null, "Name :" + data.getName() + "\nGrade" 
                                        + data.getSGrade() + "\nSchool: "
                                        + data.getSName
                                        + "\nAverage: " + average + 
                                        "\nGPA: " + gpa + ".");
       }
      }

  18. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    a method inside the main method
    That is not allowed. Methods can not be nested one inside of another.

    Methods in a class must be peers.

    Look at the tutorial for how to code methods:
    http://docs.oracle.com/javase/tutori...O/methods.html
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Gerock7 (April 22nd, 2014)

  20. #15
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    I fixed it and now I'm not getting any error but it is not working as it should be.
    I'm usign BJ and when starting the demo it jumps to the part "Enter the number of grades:"
    Then after choosing a number it prompts the respective windows but after entering the last grade it doesn't happend anything it
    looks like if the program is over idk what is wrong this is my code:
    import javax.swing.*;
    /**
     * The  ReportGenerator class uses the StudentReport1 class to simulate the report 
     * of a student. Holds fields for number of classes,grades received, average, and gpa.
     * 
     * @author Luis Fierro
     * @version 4/16/2014
     */
    public class ReportGenerator
    {
     
        /**
         * getData method creates a StudentReport1 object
         * pobulated with data from the user
         * 
         * @return A reference to StudentReport1
         */
         public static StudentReport1 getData()
        {
            //variables to hold  the information
             String name;
             String schoolGrade;
             String schoolName;
     
            //read the information
            name=JOptionPane.showInputDialog("Enter your name: ");
            schoolGrade=JOptionPane.showInputDialog("Enter your grade in school: ");
            schoolName=JOptionPane.showInputDialog("Enter the name of your school: ");
     
            //create a phoneBook entry object
            StudentReport1 data = new StudentReport1(name, schoolGrade, schoolName);
     
            //return a reference to the object
            return data;
     
        }
     
        public static void main(String [] args)
      {
     
          // declare variables
        int numOfGrades=0;       //number of classes of the student
        int grades;          //grades received in each class
        double average;                //average= sum of all grades / number of classes
        double gpa;                    //average converted to scale 1-4
        int total=0;			        //total of the grades 
    	String trash;			        //to convert string to double
     
     
     
     
     
          // Get the average
          average = getAverage(numOfGrades, total);
     
          // Get the gpa
          gpa = getGpa(average);
     
     
          //ask number of grades
          trash=JOptionPane.showInputDialog("Number of grades:");             
    		numOfGrades=Integer.parseInt(trash);				              		                                                                 
     
    	  //get the grades added together in order to calculate the average
    		for (int i = 1; i <=numOfGrades; i++) 
    				{
     
    			trash=JOptionPane.showInputDialog("Test " + i + " : " );
    			grades=Integer.parseInt(trash);
     
     
    				total+=grades;	
     
    				}
        }
     
     
       public static double getAverage(double total, int numOfClasses)
       {
          return total / numOfClasses;
     
       }
     
       public static double getGpa(double average)
       {
          return (average*4)/100;
     
       }
     
     
       /**
         * showData method shows the data stored in  
         * the  StudentReport1 object
         * 
         * @param data The data to show 
         */
     
       public static void showData(StudentReport1 data, double average, double gpa)
     
       {
          JOptionPane.showMessageDialog(null, "Name :" + data.getName() + "\nGrade" 
                                        + data.getSGrade() + "\nSchool: "
                                        + data.getSName()
                                        + "\nAverage: " + average + 
                                        "\nGPA: " + gpa + ".");
       }
    }

  21. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    Now you need to learn how to debug the code. I use the println() method to print out the values of variables as the program executes so you can see what the computer sees when it executes the program.
    Add some println() statements that print out the variables when their value is changed so you can see what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Gerock7 (April 22nd, 2014)

  23. #17
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    Could I see an example because I don't get it, like, what should I change or where should I add something

  24. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    For example print out the value of total after it has been computed:
       System.out.println("total="+total);

    What should the program do next after it has computed the total?
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    Gerock7 (April 22nd, 2014)

  26. #19
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    calculate and print average, and gpa

    --- Update ---

    Is there anyway that you can put me a example step by step because I have 1hr to turn in the code and I don't know how to fix that

  27. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    Start with the next step after the total has been computed.
    Is that compute the average? Given the total, what else does the code need to have to compute the average?
    If you don't understand my answer, don't ignore it, ask a question.

  28. #21
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    number of classes
    So it should look like
    System.out.println(total=+grades);
    Then
    System.out.println("Enter number of grades: ")

  29. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    What are you asking about?
    If you don't understand my answer, don't ignore it, ask a question.

  30. #23
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Syntax error!!!

    Sorry is just that I wanna finish this fast to start with more homework and I don't even get focused on what I'm doing.
    Ok... at the end of the code I was thinking on writing something like
    JOptionPane.showMessageDialog(null, "Name: " (This would display the information, but idk how to call it from the methods) + method + );

  31. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Syntax error!!!

    You need to compute the values before you can display them.
    For testing you can use the println() method/
    When all the values have been computed, then worry about how to display them.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Syntax error
    By Kez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2014, 04:48 PM
  2. Syntax error on Token else?
    By SgtGarrison in forum Loops & Control Statements
    Replies: 15
    Last Post: March 20th, 2013, 06:26 AM
  3. Please help not sure how to get past the Syntax error
    By KnightC in forum Other Programming Languages
    Replies: 2
    Last Post: December 23rd, 2011, 10:09 AM
  4. Syntax error with dispose()
    By jagnat in forum AWT / Java Swing
    Replies: 1
    Last Post: October 14th, 2011, 11:00 AM
  5. do while syntax error problem
    By derekxec in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 1st, 2011, 06:30 PM

Tags for this Thread