LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2003, 05:47 PM   #1
Laptop2250
Member
 
Registered: Oct 2003
Posts: 131

Rep: Reputation: 15
java, help me i tried many things no results


ok.. i have solved many problems by myself but wtf, more errors... i dont get this $hit... anyway this some code for a calculator program (in good tradition i called it Kalculator )


i recieve the following error at the //********* (near if/else's at result=)

**incompatible types- found int but expected java.lang.String**
how can i fix this? thanks in adcance, sincerly


import java.awt.*;
import javax.swing.*;

public class Kalculator extends JFrame
{
public Kalculator()
{
super("Kalculator");
Container c= getContentPane();
c.setBackground (Color.white);
c.setLayout (new FlowLayout());
c.add (new JLabel ("Kal"));
}

public static void main (String []args)
{Kalculator window= new Kalculator();
String temp,result;
EasyReader Read= new EasyReader();
System.out.print ("Select your desired operation (1,2,3,4) -->");
temp= Read.readLine();
int menuChoice= Integer.parseInt(temp);
System.out.print ("Enter the first integer:");
temp= Read.readLine();
int num1= Integer.parseInt(temp);
System.out.print ("Enter second integer:");
temp= Read.readLine();
int num2= Integer.parseInt(temp);
if (menuChoice==1)
result=num1+num2; //here is my error
if (menuChoice==2)
result=num1-num2;
if (menuChoice==3)
result=num1*num2;
if (menuChoice==4)
result=num1/num2;
else
System.out.print ("Incorrect entry. Please try again with one of the following options:/n"+
"1 adds two numbers/n"+ "2 subtracts two numbers/n"+ "3 multiplies two numbers/n"+ "4 divides two numbers");
System.out.print ("The result is"+result);
window.setSize(900, 400);
window.show();
}

}
 
Old 10-18-2003, 06:09 PM   #2
toolshed
Member
 
Registered: Aug 2001
Location: North Ka-Ki-Lak-i
Distribution: Fedora
Posts: 79

Rep: Reputation: 15
result=num1+num2; //here is my error

result is a string...and u are trying to put ints in it. do this:

result = Integer.parseInt(num1 + num2)


You need to put the code in code tags, and btw use jikes....it is the best compiler out there and will give you good error messages.
 
Old 10-18-2003, 09:52 PM   #3
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Quote:
Originally posted by toolshed
result=num1+num2; //here is my error

result is a string...and u are trying to put ints in it. do this:

result = Integer.parseInt(num1 + num2)


That will still give you an error. result is a string and you're still trying to put ints into it. You'd be better off to change result to type int. This way, when you call the following method:

result = num1+num2 // this won't give you an error,

and when you call:

System.out.println("Your result is: " + result);

result is changed into a String for you implicitly.
 
Old 10-18-2003, 10:05 PM   #4
Laptop2250
Member
 
Registered: Oct 2003
Posts: 131

Original Poster
Rep: Reputation: 15
you mean

int result= num1+num2; //?

that doesnt work, says .class expected
 
Old 10-18-2003, 10:18 PM   #5
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
I mean

int result = Integer.parseInt(num1 + num2); //that will work

Print the exact error you're getting. .class expected doesnt' sound like it has anything to do with the aforementioned code.
 
Old 10-19-2003, 12:33 PM   #6
Laptop2250
Member
 
Registered: Oct 2003
Posts: 131

Original Poster
Rep: Reputation: 15
It tried it int result = Integer.parseInt(num1 + num2); still doesnt work.
the exact error message is " '.class' expected" then i can click on a ? for more info and that says " '.class' expected No more information"

I use BlueJ as a compiler, but im switching to something else soon. I also downloaded j2sdk1.4.2 from java.sun.com
 
Old 10-19-2003, 04:00 PM   #7
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
I'm using the compiler from Sun. I've never heard of that error.

int result = Ingteger.parseInt(num1 + num2);

works fine on my computer.
 
Old 10-19-2003, 10:14 PM   #8
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
BlueJ is not a compiler. It's a modelling tool for student.
 
Old 10-20-2003, 12:29 PM   #9
Laptop2250
Member
 
Registered: Oct 2003
Posts: 131

Original Poster
Rep: Reputation: 15
ok thats it, i didnt want to (have to install), but ill have to, im whipin out the java forte
 
Old 10-26-2003, 05:12 PM   #10
Laptop2250
Member
 
Registered: Oct 2003
Posts: 131

Original Poster
Rep: Reputation: 15
ok thanks for all the "help" guys anyway, i couldnt find the forte so i went back to the code and to fix this all i had to do was make result=0 when i first declared it...

wtf u know im a BIG NNEEEWWBBBBIEEEE and i come to this forum and find window-like answers (reinstall it, diff compiler)

be ashamed

switch to windows guys code is 2 much for u ppl, u need the double click feature
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Suse: confused on many things, why is installing things so hard? blackflare Linux - Newbie 11 10-16-2007 04:35 AM
A possible bug - please try for results aikidoist72 Ubuntu 2 04-11-2005 06:53 PM
Same code, two different results. (C++) Travis86 Programming 7 11-07-2004 04:36 PM
Same C code different results Veteq Programming 17 07-24-2004 03:26 AM
Things were fine! (java problems) ch4s3r Linux - Software 2 10-26-2003 11:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration