Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For the Love of Loops!

 
Marie Jeanne Thibault
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Before you look at my code, let me warn you that I know it's a mess...I'm suppose to create a very simple calculator that uses command line parameters. I'm not suppose to use any exception handlers but it is suppose to display a message when one of the operands is not an integer or if there are less or more than three args.
I've got all sorts of "if" loops going on. It compiles but is not running properly. Any guidance would be appreciated.
public class ExceptDemo1b
{
//Main Method with three arguments
//args[0]: operator
//args[1]: operand1
//args[2]: operand2
public static void main(String[] args)
{
//Declare and initialize variables
int result = 0;
int operand1 = Integer.parseInt(args[1]);
int operand2 = Integer.parseInt(args[2]);
if (args.length != 3)
{
System.out.println("Usage: java Calculator operator operand1 operand2");
System.exit(0);
}
if((operand1 >= 0) || (operand1 < 0))
{
switch (args[0].charAt(0))
{
case '+': result = Integer.parseInt(args[1]) +
Integer.parseInt(args[2]);
break;
case '-': result = Integer.parseInt(args[1]) -
Integer.parseInt(args[2]);
break;

case '*': result = Integer.parseInt(args[1]) *
Integer.parseInt(args[2]);
break;
case '/': result = Integer.parseInt(args[1]) /
Integer.parseInt(args[2]);
}
//Display the result
System.out.println(args[1]+ ' ' +args[0]+ ' ' +args[2]+ "=" +result);
}
else
{
System.out.println("Wrong input " + operand1);
System.exit(0);
}
if ((operand2 >= 0) || (operand2 < 0))

switch (args[0].charAt(0))
{
case '+': result = Integer.parseInt(args[1]) +
Integer.parseInt(args[2]);
break;
case '-': result = Integer.parseInt(args[1]) -
Integer.parseInt(args[2]);
break;

case '*': result = Integer.parseInt(args[1]) *
Integer.parseInt(args[2]);
break;
case '/': result = Integer.parseInt(args[1]) /
Integer.parseInt(args[2]);
}
//Display the result
System.out.println(args[1]+ ' ' +args[0]+ ' ' +args[2]+ "=" +result);


System.out.println("Wrong input " + operand2);
System.exit(0);
}
}
Thanks,
Marie
 
frank vallee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marie
Here are some things I found by a rapid check of your code:
1 - check the number of arguments at the very beggining of the program.
2 - keep your main clean (ie: put the large chunk of code in a method)
3 - watch for illegal operation (divide by zero)
4 - you declared variables operand1 and operand2 but still use parseInt() in program. Why ?
5 - you calculate the results twice
6 - indent you code and yo will see why it always write "Wrong input at the end"
In the future, try to avoid big chunks of unclean code in forum topics. People have more chance to reply if they can answer rapidly.
Francois
 
Nils Hofmann
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi marie,
first, it seems that there is a general problem with the '*'-operator as argument (at least at windows), other operators work.
your program calculates everything twice because both expressions
if((operand1 >= 0) || (operand1 < 0)) and
if ((operand2 >= 0) || (operand2 < 0))
are always true.
the 'wrong input' message is always printed because it is not included into an else-statement.
nils
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic