ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to make a graphical program in Java (my first shot...ghehe) and I'm having a little hard time in here.
The program is a very simple calculator. It asks for 3 user's input. The first one is a number, the second is an operator ( + - * /) and the last one is another number. As it's very hard-coded, I'm trying to get it to run first
Code:
// import API-JOptionPane which holds the dialog boxes
import javax.swing.JOptionPane;
class Calc {
public static void main ( String[] arguments ) {
String userInput1, userInput2, userOperator;
double userTotal;
// Display a message to the user:
JOptionPane.showMessageDialog( null, "Welcome to my hard-coded Program!");
userInput1 = JOptionPane.showInputDialog ( null, "Insert a number" );
userOperator = JOptionPane.showInputDialog ( null, "What to do?");
userInput2 = JOptionPane.showInputDialog ( "Insert a second number" );
// ----- Program comes here ---- //
//userTotal = Integer.parseInt(userInput1) + Integer.parseInt(userInput2);
JOptionPane.showMessageDialog ( null, userInput1 + " " + userOperator + " " +
userInput2 + " = " + userTotal);
}
}
The problem is... Me, with all my lack of wisdom, have no idea how to make the user "userOperator" to perform a math operation. I know that in Java, all user's inputs are in Strings form, so I've to convert it (parseInt) into integers to make math operations. What I cannot think is how to make the operator (userOperator) to work together with both user's input (userInput1 and userInput2). The uncommented line:
You may want to use a switch statement to evaluate the value of your userOperator compared to the various functions that you would want to implement on your calculator.
You would have to parse your userOperator into a char and compare it to all of your operators as chars like so:
almost there. I still fall into the converting problem. I know that Java can parse String to Integer, but I cannot make a String into a Char. Here is how the program looks like now:
Code:
// import API-JOptionPane which holds the dialog boxes
import javax.swing.JOptionPane;
class Calc {
public static void main ( String[] arguments ) {
String userInput1, userInput2, userOperator;
double userTotal;
// Display a message to the user:
JOptionPane.showMessageDialog( null, "Welcome to my hard-coded Program!");
userInput1 = JOptionPane.showInputDialog ( null, "Insert a number" );
userOperator = JOptionPane.showInputDialog ( null, "What to do?");
userInput2 = JOptionPane.showInputDialog ( null, "Insert a second number" );
switch (userOperator)
{
case '+':
userTotal = Integer.parseInt(userInput1) + Integer.parseInt(userInput2) ;
break;
case '-':
userTotal = Integer.parseInt(userInput1) - Integer.parseInt(userInput2) ;
break;
case '*':
userTotal = Integer.parseInt(userInput1) * Integer.parseInt(userInput2) ;
break;
case '/':
userTotal = Integer.parseInt(userInput1) / Integer.parseInt(userInput2) ;
break;
default:
JOptionPane.showMessageDialog ( null, "Oh no, another bug :'( " );
break;
}
JOptionPane.showMessageDialog ( null, userInput1 + " " + userOperator + " " + userInput2 + " = " + userTotal);
}
}
I've tried something like someChar = (char)userOperator; but it fails miserably :
Error: incompatible types
found : java.lang.String
required: int
Is it possible to convert/parse a String into a char?... I've to buy some programming books . Definitely...
Thanks a lot ter_roshak. I really appreciated your effort on this . Java is a real pain sometimes . Apparently this type of convertion ain't possible. It accepts Strings to Integer, so I was thinking to get that integer, convert it explicit to char and make the case: based on ASCI code. It did not work. I've tried to do the same in C++;
Code:
// The same thing, but in C++
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
// Declare variables
char userInput1, userInput2, userOperator;
double userTotal;
// Print a message to the user
cout << "Welcome to my buggy program :)\n";
cout << " Insert a number\n ";
cin >> userInput1;
cout << " What to you want do with it? ( + - * / )";
cin >> userOperator;
cout << " \nInsert a second number\n" ;
cin >> userInput2;
// this is just a test, use always "+" or else it will fail.
// I'd add the other cases later... but since not even 43 is working...
// there's no need for it :)
switch ((int)userOperator)
{
case 43:
{
userTotal = (int)userInput1 + (int)userInput2;
cout << userTotal;
break;
}
default:
cout << "Oh no, another bug :'( ";
break;
}
return 0;
}
also did not work either. When I use explicit converting, it converts the char to integers and indeed gives a strange wrong result. Although, I've to get this working in Java, just thought that more peoples could see the actual problem . The bad thing is that the only way to get user's input in Java is... either console input (when they type the name of the program, followed by arguments) or with dialog boxes (as far as I know). And in both cases, it returns a String . I'm very very lost
Last edited by Mega Man X; 01-03-2004 at 08:24 AM.
hmm.. isn't there an String.charat() function in Java?
The main diffrence in C/Java is that in C/++ an string is an array of characters, and in java an string is an object, so you need to use String.charAt() or something like that..
Yeah!!! That surely did the trick. I've placed userOperator.charAt(0) at my switch-case line and now it works as it should. Pretty nice to see "graphical" under Java .
It's cool the amount of functions Java actually has. I was not aware of charAt, but I was with .substring, .indexOf and .toUpperCase. God dammit, it's just too much . I will get some good Java books. Perhaps one of the Sams Teach yourself series Or else I doubt that Java and I can be good friends and get along. lol
Thanks a lot for the help ter_roshak and Langly. I could not make this without you guys.
Thanks to you guys, I'm improving the program. Now I've divided the program in functions, and a new object is created. If I don't do that, I cannot learn Java, since I was trowing everything into a C++ like style and that's not the point of Java. Here is what I have so far:
Code:
import javax.swing.JOptionPane;
class Calculator {
String userInput1, userOperator, userInput2;
double userTotal = 0.0;
double makeCalculations() {
switch ( userOperator.charAt(0) )
{
case '+':
userTotal = Integer.parseInt(userInput1) + Integer.parseInt(userInput2) ;
break;
case '-':
userTotal = Integer.parseInt(userInput1) - Integer.parseInt(userInput2) ;
break;
case '*':
userTotal = Integer.parseInt(userInput1) * Integer.parseInt(userInput2) ;
break;
case '/':
userTotal = Integer.parseInt(userInput1) / Integer.parseInt(userInput2) ;
break;
default:
JOptionPane.showMessageDialog ( null, "ghehe, another bug :)" );
break;
}
return userTotal;
}
void printOut () {
JOptionPane.showMessageDialog ( null, userInput1 + " "
+ userOperator + " "
+ userInput2 + " = "
+ userTotal);
}
public static void main ( String[] argument )
{
Calculator myCalculator = new Calculator();
myCalculator.userInput1 = JOptionPane.showInputDialog ( null, "Insert the First Number" );
myCalculator.userOperator = JOptionPane.showInputDialog ( null, "Which operation do to perform?" );
myCalculator.userInput2 = JOptionPane.showInputDialog ( null, "Insert the Second Number" );
myCalculator.makeCalculations ();
myCalculator.printOut ();
}
}
It's still not good, but Java is not so easy to play with . But it's getting a better shape
Last edited by Mega Man X; 01-03-2004 at 03:18 PM.
if you had looked at the String class, it would have told you about charAt
let me know any other modifications your after for your calculator, and il try and help out all i can (iv written a calculator in Java before, although the source for it is long since deleted )
as for books, the Java in 21 days is a great book Within a week of reading i was writing graphical and networking applications
It's nice to see that you guys also are interested in learning ( and teaching me too, thanks mate ) Java . By the way, you both working pretty "syncronized". Almost the same answer with a minute of difference... ghehe.
I've downloaded that document to my harddrive this morning . I never though it was that well documented. Makes funny of M$ Visual Studio 6.0 which on CD is to install that slow, expensive junk, and 2 (in word, two) CD's for the MSDN ( the crappy help files...).
I was running my program now and I've faced on problem... what if the user divides a number by 0?. I've first tried to:
if userInput2 == 0
userTotal = 0;
but that's lame, and would make the application bad, since you can make 2-0, 2+0, 2*0. But if you do 2/0, you got an invalid statement... Is there anyway to get around it?
Funny isn't it? Sun did all they could to make it easier, but I still got stuck into logical operations... I'm not a wise man :S
just break that case statement up a little. Take the second integer and test if it is zero, if its not you can divide as normal, or if it is make a nice little popup or something
off the top of my head: (following code isnt tested or anything)
Code:
case '/':
if (Integer.parseInt(userInput2) == 0) {
JOptionDialog.showMessageDialog(null, "oh no you dont!");
break;
} else {
userTotal = Integer.parseInt(userInput1) / Integer.parseInt(userInput2) ;
break;
}
i havent tested that or anything, but hopefully you get the jist
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.