The Artima Developer Community
Sponsored Link

Java Answers Forum
Using Loops in place of Exception Handlers

2 replies on 1 page. Most recent reply: Mar 8, 2004 3:31 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Lynn Robertson

Posts: 4
Nickname: looloo
Registered: Dec, 2003

Using Loops in place of Exception Handlers Posted: Mar 7, 2004 8:09 PM
Reply to this message Reply
Advertisement
Please help me...I have to write a program that will only accept integers as input for operand 1 and 2. I'm not allowed to use exception handlers and the offending input is suppose to be identified. I've included my code but I've written out the last part to describe what I'd like it to do. I'm sure it's probably something really simple but I'm pulling my hair out.
Thanks,
Lynn

//ExceptDemo1b.java

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 = 0;
int operand2 = 0;


if (args.length != 3)
{
System.out.println("Usage: java Calculator operator operand1 operand2");
System.exit(0);
}
if(((operand1 >= 0) || (operand1 < 0)) && ((operand2 >= 0) || (operand2 < 0)))
operand1 = Integer.parseInt(args[1]);
operand2 = Integer.parseInt(args[2]);
{
if (args[0].equals("+"))
{
result = operand1 + operand2;
System.out.println(args[1] + ' ' + args[0] + ' ' +args[2] + "=" + result);
}

else if (args[0].equals("-"))
{
result = operand1 - operand2;
System.out.println(args[1] + ' ' + args[0] + ' ' +args[2] + "=" + result);
}

else if (args[0].equals("*"))
{
result = operand1 * operand2;
System.out.println(args[1] + ' ' + args[0] + ' ' +args[2] + "=" + result);
}

else if (args[0].equals("/"))
{
result = operand1 / operand2;
System.out.println(args[1] + ' ' + args[0] + ' ' +args[2] + "=" + result);
}
}

if(operand 1 or operand2 is not an integer)
{
System.out.println("Input must be an Integer" + operand1 or operand2);
}
}
}


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Using Loops in place of Exception Handlers Posted: Mar 8, 2004 5:50 AM
Reply to this message Reply
Since you mention loops in your Subject Line, I'm going to guess that the assignment requires you to use a loop. Maybe you are supposed to use a loop to examine the characters in each argument. That way you could verify that they are all numerical characters. Something like this
boolean allNumeric = true;
for(int i = 0; i < args[1].length(); i++)
{
     char letter = args[1].charAt(i);
     allNumeric = ((letter >='0') && (letter <= '9'));
}
//repeat for args[2]
if(allNumeric)
   //do the arithmetic
else
  //tell user that one of the arguments contains non-numeric characters.


I hope this helps.
twc

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Using Loops in place of Exception Handlers Posted: Mar 8, 2004 3:31 PM
Reply to this message Reply
Very similar to your code, but I prefere it that way :

String numbers = "0123456789";
boolean allNumeric = true;
char letter;
for(int i = 0; i < args[1].length(); i++)
{
     letter = args[1].charAt(i);
     if ( numbers.indexOf (letter) == -1)
     {
          allNumeric = false;
          break; // Must break otherwise it is only the result of the last check 
                    // that will be taken into account !
     }
}
 
if(allNumeric)
{
   //do the arithmetic
} else
{
  //tell user that one of the arguments contains non-numeric characters.
}
 
//repeat for the following argument in a 'bigger' loop


Cheers,

\T,

Flat View: This topic has 2 replies on 1 page
Topic: Re-stated More Clearly: Anononymous Local Inner Class Question Previous Topic   Next Topic Topic: NYC Study Group Anyone?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use