6 Replies - 1186 Views - Last Post: 21 February 2010 - 05:19 PM Rate Topic: -----

#1 pyro1772  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-February 10

Racking my brain and still can't figure this out

Posted 20 February 2010 - 09:58 AM

I'm new to ava programming and having a tough go at this. I keep getting error messages back from the program when I compile it and can not understand why. I followed along with the book, but the book is written to where it is pretty much useless. Can anyone help?
Here is my code it is for an application that does multiplication quizes.
/*
   Chapter 4:	Programming Assignment 1
   Programmer:	Richard Mullikin
   Date:		February 19, 2010
   Filename: 	Quiz.java
   Purpose:
*/

import java.io.*;

public class Quiz
{
  public static void main (String[] args ) throws IOException
  {
    //Declaring variables
    int multiplier;
    int correct;
    boolean done = false;

    //Opening messages
    System.out.println("\t\tWelcome to the Multiplication Quiz");
    System.out.println("");

	try
	{
    	//Calling the user-defined methods
    	multiplier = getNumber();
    	correct = takeQuiz(multiplier);
    	System.out.println("\t\tYou got "+correct+ " correct!" );
  	}
  	catch(NumberFormatException e)
  	{
		JOptionPane.showMessageDialog(null,"Your entry was not valid.","Error",JOptionPane.INFORMATION_MESSAGE);
	}

   public static int getNumber() throws IOException
   {
      //Declaring variables
      BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
      String inputData;
      int multiplier;

      //Get a value from user
      System.out.print("Enter the multiplication table you wish to practice: ");
         inputData = dataIn.readLine();
         multiplier = Integer.parseInt( inputData );

      //Return a value to main
      return multiplier;
   }

   public static int takeQuiz(int multiplier) throws IOException
   {
     //Declaring variables
      BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
      String inputData;
      int answer;
      int count = 0;
      int correct = 0;

      while ( count <= 12)
      {
         //Display question and get answer
         System.out.println( "What is "+count + " times " + multiplier +"?" );
            inputData = dataIn.readLine();
            answer     = Integer.parseInt( inputData );

         if (answer == count * multiplier)
         {
            System.out.println("\tCorrect!");
	    	correct = correct + 1;
         }

         else
         {
           	System.out.println("\tIncorrect");
         }

         count = count + 1;
      }
      return correct;
}





the errors I get back is this.

C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:36: illegal start of expression
public static int getNumber()
^
C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:36: illegal start of expression
public static int getNumber()
^
C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:36: ';' expected
public static int getNumber()
^
C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:36: ';' expected
public static int getNumber()
^
C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:52: illegal start of expression
public static int takeQuiz(int multiplier) throws IOException;
^
C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:52: illegal start of expression
public static int takeQuiz(int multiplier) throws IOException;
^
C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:52: ';' expected
public static int takeQuiz(int multiplier) throws IOException;
^
C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:52: '.class' expected
public static int takeQuiz(int multiplier) throws IOException;
^
C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:52: ';' expected
public static int takeQuiz(int multiplier) throws IOException;
^
C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:52: illegal start of expression
public static int takeQuiz(int multiplier) throws IOException;
^
C:\Users\Richard & Julie\Desktop\School Stuff\Java Programing\Quiz.java:82: reached end of file while parsing
}
^
11 errors

Tool completed with exit code 1

They seem to be all focused on lines 36 and 52. These were not the lines I had to modify either, they came from the data files. Anysugestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Racking my brain and still can't figure this out

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,168
  • Joined: 27-December 08

Re: Racking my brain and still can't figure this out

Posted 20 February 2010 - 10:26 AM

Your problem is that you declare your methods inside the main() method. This is illegal. They should be declared in the class, but not a method. Like so:
public class Quiz{
   public static void main(String[] args){
     //implementation not shown
   }

   public static int getNumber(){
    //implementation not shown
   }

   public static int takeQuiz(int multiplier) throws IOException{
     //code
   }
}



Also, only abstract methods can end in a semicolon when defining them. So if you declared takeQuiz as abstract, you would end it in a semi-colon, like so. However, if you don't know what an abstract method is, don't worry about it. Just understand that you can't end method definitions with semi-colons. Instead, you use curly-braces and place the method code inside the braces.
public static abstract int takeQuiz(int multiplier) throws IOException;



Lastly, when you invoke a method, you end the statement with a semi-colon. So if you invoke takeQuiz() and assign it to an int variable, you would end that statement like so:

int x = 0;
//use try-catch b/c takeQuiz() throws an Exception
try{
  x = takeQuiz(3);
}
catch(IOException e){
  System.out.println(e);
}


Was This Post Helpful? 1
  • +
  • -

#3 pyro1772  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-February 10

Re: Racking my brain and still can't figure this out

Posted 21 February 2010 - 12:06 PM

Ok....I have to admit I'm way confused now. I guess I'm just not getting what your telling me.
Was This Post Helpful? 0
  • +
  • -

#4 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Racking my brain and still can't figure this out

Posted 21 February 2010 - 12:15 PM

macosxnerd101 explained to you very clearly.

You cannot create methods inside the main method.

Everything i would have said, macosxnerd101 has already mentioned.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,168
  • Joined: 27-December 08

Re: Racking my brain and still can't figure this out

Posted 21 February 2010 - 01:04 PM

@pyro1772: What exactly don't you understand?
Was This Post Helpful? 0
  • +
  • -

#6 pyro1772  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-February 10

Re: Racking my brain and still can't figure this out

Posted 21 February 2010 - 02:51 PM

I really meant no disrespect to your answer. I'm really just blown away by all this and once I feel i understand the methods I look at it and it looks like a foreign language. I read back over the chapter again, and fixed the problem like you said and it worked. I guess I'm just frustrated because this syuff usually comes to me a lot quicker. Thanks again man.

I really meant no disrespect to your answer. I'm really just blown away by all this and once I feel i understand the methods I look at it and it looks like a foreign language. I read back over the chapter again, and fixed the problem like you said and it worked. I guess I'm just frustrated because this stuff usually comes to me a lot quicker. Thanks again man.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Racking my brain and still can't figure this out

Posted 21 February 2010 - 05:19 PM

Alright, you cannot place a method *inside* another method. You can only stick methods in classes. You start and end method using the { and } characters, so if you start a method, you have to end it before you can make another. For example, this code is WRONG!:

class Whatev {
    // Started this method at the {
    public static void main(string[] args) {
        ...

        // Started this method at the {
        public static void add() {
  
        } // end add() method
    } // end main() method
} // end class



See? I placed another method in the code before I ended the first method. You can ONLY put another method after you have ENDED the one before it. Like so:

 
class Whatev {
    // Started this method at the {
    public static void main(string[] args) {
        ...
    } // end main() method

    // Started this method AFTER the last method was done
    public static void add() {
        ...
    } // end add() method
} // end class



Does that help at all?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1