This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes For the Love of Loops! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For the Love of Loops!" Watch "For the Love of Loops!" New topic
Author

For the Love of Loops!

Marie Jeanne Thibault
Greenhorn

Joined: Nov 15, 2003
Posts: 15
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

Joined: Jan 10, 2004
Posts: 8
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

Joined: Jan 10, 2004
Posts: 9
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For the Love of Loops!
 
Similar Threads
Code without try-catch, just if/else or other
String arguments
Help! Command-line arguments
using string tokenizer to retrieve rationals
Exception Handling