Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

For the Love of Loops!

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic