Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Illegal start of expression

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This program is a mathematical quiz .........



I get the following error :


----jGRASP exec: javac -g C:\Java Working Directory\MathTest1.java

MathTest1.java:67: illegal start of expression
public void init()
^
MathTest1.java:67: illegal start of expression
public void init()
^
MathTest1.java:67: ';' expected
public void init()
^
MathTest1.java:115: illegal start of expression
public void paint(Graphics gr)
^
MathTest1.java:115: illegal start of expression
public void paint(Graphics gr)
^
MathTest1.java:115: ';' expected
public void paint(Graphics gr)
^
MathTest1.java:115: ';' expected
public void paint(Graphics gr)
^
MathTest1.java:157: illegal start of expression
public void itemStateChanged(ItemEvent e)
^
MathTest1.java:157: illegal start of expression
public void itemStateChanged(ItemEvent e)
^
MathTest1.java:157: ';' expected
public void itemStateChanged(ItemEvent e)
^
MathTest1.java:157: ';' expected
public void itemStateChanged(ItemEvent e)
^
11 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.



Thanks,
Radhika
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check your indentation.
You have put all your methods inside main.

If you are using a simple editor which does not handle indentation for you, it is always a good idea to declare the methods first, curly braces and all, and then write the actual implementation.
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

MathTest1.java:67: illegal start of expression
public void init()



Check just before this line.

Also I agree with what Maneesh says, indentation makes the code more easily readable and helps you spot errors.

Sean
 
Radhika Srinivasan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me see if I'm right I've removed the main method... I was missing this

import java.awt.event.*;

 
Radhika Srinivasan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The check boxes doesn't appear could you help me with it....

Thanks,
Radhika
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Radhika Srinivasan wrote:The check boxes doesn't appear could you help me with it....

Thanks,
Radhika



Help with what? You tried to declare a method inside another method. Don't do that.

And what is this "check boxes" fix you are attempting?

Henry
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read what Maneesh wrote?

You cannot declare methods inside methods. What you did, is put the init() method inside the main() method. That doesn't work. Did you forget a } at the end of the main() method, just before the init() method?
 
Radhika Srinivasan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried "}" before init() it doesn't work since I have declared. So I've removed the main method. But the checkboxes which includes answer doesn't appear in the applet.

I'm totally confused.., Please help me getting it right........

Thanks,
Radhika
 
Radhika Srinivasan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my 1st project when I execute it works this way in commad prompt mode, in the present project I've modified it to work in applet.
----jGRASP exec: java MathTest

1. 7+2 = ? a.6 b.8 c.9 d.10
Enter Your Answer:
c
Correct
2. 9-5 = ? a.2 b.4 c.6 d.8
Enter Your Answer:
b
Correct
3. 144/12 = ? a.10 b.12 c.14 d.16
Enter Your Answer:
b
Correct
4. 5*5 = ? a.10 b.20 c.25 d.125
Enter Your Answer:
c
Correct
5. 2+3+5 = ? a.7 b.8 c.9 d.10
Enter Your Answer:
d
Correct

You had 5 question correct.

Congratulations,you passed with a score of 100%


----jGRASP: operation complete.
I have included my 1st project :
 
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand. Does it work or not? do you have a problem or don't you?

I'm not trying to be rude... I just can't figure out what help (if any) you need. Your original post said "I'm getting this error". Several people told you what was wrong, but it doesn't look like you took their advice or clarified what the issue was.

My suggestion would be this:

State VERY CLEARLY what the exact problem is. Don't post 200 lines of code and say "There is a problem here somewhere", but only the relevant portion of the code DIRECTLY related to your problem.
 
Radhika Srinivasan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got rid of the problem "Illegal start of expression" by including

import java.awt.event.*;

as well I removed the methods which were inside the main method.

Presently I'm having problem with "the checkboxes along with the answers" appearing on the screen to select the choices of answers from the quiz question.

Thanks,
Radhika
 
Radhika Srinivasan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check boxes along with answers doesn't appear on the screen.
 
Evil is afoot. But this tiny ad is just an ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic