Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2007, 11:29 PM
Member
 
Join Date: Jul 2007
Posts: 26
romina is on a distinguished road
Error: '{' expected
Hello, I have this stupid error. I would probably laugh at myself, because my error is "{" missing on like 34. It is pretty simple I would assume, but I cant figure out what I'm missing. The big thing I have noticed in Java in that everything has to be in classes, so I'm not sure if my set up is right. But yeah, I'm pretty much trying to code this program like I would in C++.

Code:
> Executing: C:\bin\javac.exe" "C:\dsp1.java" C:\dsp1.java:34: '{' expected public class fraction() { ^ 1 error > Execution finished.
Code:
import java.util.Scanner; public class dsp1 { public static void main(String[] args) { fraction x=new fraction(), y=new fraction(), result=null; String op; Scanner kb=new Scanner(System.in); System.out.print("please enter an expression int int op int int\n"); System.out.print("for example, 3 4 - 2 3 \n"); System.out.print("where 3 4 and 2 3 represent the fractions 3/4 and 2/3:\n"); x.read(kb); //read the first fraction op=kb.next(); //read the operator y.read(kb); //read the second fraction switch(op.charAt(0)) { case '+': result=x.plus(y); break; case '-': result=x.minus(y); break; case '*': result=x.times(y); break; case '/': result=x.div(y); break; default: System.out.println("incorrect operator"); System.exit(1); } if (result!=null) System.out.print(x+" "+op+" "+y+" = "+result+"\n"); } //end main() } //end dsp1 public class fraction() { public int num, den; public void read(); public fraction plus(fraction); /*public fraction minus(fraction); public fraction times(fraction); public fraction div(fraction);*/ public void lcf(); void read(){ num = kb.next(); den = kb.next(); return; } fraction plus(fraction k){ fraction temp; temp.den = den * k.den; temp.num = (num*k.den)+(k.num*den); temp.lcf(); return temp; } void lcf(){ for(int x = den;x > 0;x--){ if(den%x == 0 && num%x == 0){ den = den/x; num = num/x; } } } }
Do I have to define kb for both classes, or just main?
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-26-2007, 11:34 AM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
The simple answer is yes you do as they are different classes. You could, however, pass the scanner into the read function:
Code:
public void read(Scanner kb) { ..... }
You must have the code for the fraction class in its own file called fraction.java which is what has probably confused you.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Identifier expected error vasu18 New To Java 1 01-01-2008 07:49 PM
Error: ')' expected baltimore New To Java 1 08-07-2007 08:32 AM
Error: <identifier> expected barney AWT / Swing 2 07-31-2007 09:38 AM
My error is: ')' expected silvia New To Java 1 07-18-2007 06:49 PM
MSG ERROR: : expected Marty New To Java 1 05-31-2007 04:21 AM


All times are GMT +3. The time now is 11:02 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org