Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-15-2006, 12:10 PM   #1 (permalink)
awady85
Recruit
 
Join Date: Nov 2006
Posts: 1
awady85 is on a distinguished road
thank u if u help me

hello all

first thank all for ur effort in this website it is a fantastic work .....

second i want ur help because i'm a beginner student in java, i just know about java for 3 hours ago and i wnt to make a calculator so ii try to write code and it not work so hope u help me, the problem is the code dont give me result just give me 0 in all time.
note: i just do the addetion part because the lest will repeat like it.


Code:
import javax.swing.*;
               

public class Calc {
	
	public static void main(String[] args) {
		
    String s1=JOptionPane.showInputDialog(null,"Enter First Num\n");
    String s2=JOptionPane.showInputDialog(null,"Enter The Sign\n");
    String s3=JOptionPane.showInputDialog(null,"Enter The Second Num\n");
    int result;
    int x1=Integer.parseInt(s1);
    int x2=Integer.parseInt(s3);
    result=0;
	if (s2==("+"))
		result=x1+x2;
		JOptionPane.showMessageDialog(null,""+result);
	}

}
awady85 is offline   Reply With Quote
Old 11-16-2006, 01:01 PM   #2 (permalink)
spidermo
Recruit
 
Join Date: Jun 2006
Posts: 14
spidermo is on a distinguished road
Try if(s2.equals("+")) instead. == compares objects, you are comparing a character.
spidermo is offline   Reply With Quote
Old 11-16-2006, 05:42 PM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Spidermo nailed it on the head. In Java, you can't use "==" for string comparison.

Also, as a matter of style, the way you have your "if" statement formatted makes it look like the JOptionPane statement is underneath included. You might want to clean it up a bit.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

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

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



All times are GMT -8. The time now is 11:49 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting