Solved

Calculator...excception

Posted on 2011-10-07
Medium Priority
286 Views
argument like:

>java Exercise13_1a 4/5 + 7

should prompt: Wrong input: 4/5

but it gives:

java.lang.NumberFormatException: For input string: "3/6"   <-- needs to read: Wrong input: 4/5
Usage: java Calculator operand1 operator operand2   <-- not necessary to show
``````import java.text.DecimalFormat;
import java.util.IllegalFormatException;

public class Exercise13_1a {

public static void isNumeric(String s) throws NumberFormatException {

boolean b = true;

try {
float f = Float.parseFloat(s);
}
catch(NumberFormatException ex){
b = false;
throw ex;
}
//return b;
}

public static void  validData(String [] ss) throws
InvalidDataException, NumberFormatException {

String temp="";

try {
isNumeric(ss[0]);
isNumeric(ss[2]);
}catch(IllegalFormatException ex){
throw ex;
}

if(Double.parseDouble(ss[2])==0.0 && ss[1].charAt(0)=='/') {
temp= "Division by zero";
}

if(temp.trim().length() >0)throw new InvalidDataException(" Input invalid: division by zero");
//return temp;
}

public static String validInput(String [] ss){

//                 for(String s: ss){
//                System.out.println(s);
//            }

String temp="";

//            System.out.println("Im here");
if (ss.length !=3) {
temp = "Usage: java Calculator operand1 operator operand2\n";

}else if(!ss[1].equals("+") && !ss[1].equals("-") && !ss
[1].equals("*") && !ss[1].equals("/")){
temp = "Usage: java Calculator operand1 operator operand2\n"+
"       (Make sure to have * inside \"*\" as shown here.";}

return temp;
}

public static void main(String[] args) {

//            for(String s: args){
//                System.out.println(s);
//            }

String errorMessage = validInput(args);

//System.out.println("Test return: "+errorMessage+"\n");

if(errorMessage.trim().length() != 0){

System.out.println(errorMessage+"\n");

}else{
try{
validData(args);
}catch(InvalidDataException ex){
System.out.println( ex.toString());
//  ex.printStackTrace();
System.out.println("Error: " + errorMessage + "\n\n" +
"Usage: java Calculator operand1 operator operand2\n");
System.exit(0);
}  catch( NumberFormatException ex1){
System.out.println( ex1.toString());
//   ex1.printStackTrace();
System.out.println(
"Usage: java Calculator operand1 operator operand2\n");
System.exit(0);

}
printTheCalculationresult(args);
}
}

public static void printTheCalculationresult(String[] args)
{
System.out.print("print The Calculation result: ");

DecimalFormat df=new DecimalFormat("0.##");
double result=0.0;

switch (args[1].charAt(0)){
case '+': result=(int) Double.parseDouble(args[0]) +
(int) Double.parseDouble(args[2]);
break;
case '-': result=Integer.parseInt(args[0]) -
Integer.parseInt(args[2]);
break;
case '*': result=(int)Double.parseDouble(args[0])*
(int) Double.parseDouble(args[2]);
break;
case '/': result=Integer.parseInt(args[0]) /
Integer.parseInt(args[2]);
}

System.out.print(args[0]+ " " +    args[1] + " " + args[2] + " = "+  df.format(result)+"\n\n");

}
}

class InvalidDataException extends Exception { String mistake;
public  InvalidDataException(String err)
{
super(err);     // call super class constructor
mistake = err;  // save message
}

public String toString(){
return mistake;
}
}
``````
0
Question by:Mike Eghtebas
• 4
• 4

LVL 47

Expert Comment

ID: 36934790
Mind that I renamed
InvalidDataException to InvalidDataException1

``````import java.text.DecimalFormat;
import java.util.IllegalFormatException;

public class Exercise13_1a {

public static void isNumeric(String s) throws NumberFormatException {

boolean b = true;

try {
float f = Float.parseFloat(s);
}
catch(NumberFormatException ex){
b = false;
throw ex;
}
//return b;
}

public static void  validData(String [] ss) throws
InvalidDataException1, NumberFormatException {

String temp="";

try {
isNumeric(ss[0]);
isNumeric(ss[2]);
}catch(NumberFormatException ex){
throw new InvalidDataException1 ("One of the operands wrong :  " + ss[0] + " " + ss[2]);
}

if(Double.parseDouble(ss[2])==0.0 && ss[1].charAt(0)=='/') {
temp= "Division by zero";
}

if(temp.trim().length() >0)throw new InvalidDataException1(" Input invalid: division by zero");
//return temp;
}

public static String validInput(String [] ss){

//                 for(String s: ss){
//                System.out.println(s);
//            }

String temp="";

//            System.out.println("Im here");
if (ss.length !=3) {
temp = "Usage: java Calculator operand1 operator operand2\n";

}else if(!ss[1].equals("+") && !ss[1].equals("-") && !ss
[1].equals("*") && !ss[1].equals("/")){
temp = "Usage: java Calculator operand1 operator operand2\n"+
"       (Make sure to have * inside \"*\" as shown here.";}

return temp;
}

public static void main(String[] args) {

//            for(String s: args){
//                System.out.println(s);
//            }

String errorMessage = validInput(args);

//System.out.println("Test return: "+errorMessage+"\n");

if(errorMessage.trim().length() != 0){

System.out.println(errorMessage+"\n");

}else{
try{
validData(args);
}catch(InvalidDataException1 ex){
System.out.println( ex.toString());
//  ex.printStackTrace();
System.out.println(
"Usage: java Calculator operand1 operator operand2\n");
System.exit(0);
}  catch( NumberFormatException ex1){
System.out.println( ex1.toString());
//   ex1.printStackTrace();
System.out.println(
"Usage: java Calculator operand1 operator operand2\n");
System.exit(0);

}
printTheCalculationresult(args);
}
}

public static void printTheCalculationresult(String[] args)
{
System.out.print("print The Calculation result: ");

DecimalFormat df=new DecimalFormat("0.##");
double result=0.0;

switch (args[1].charAt(0)){
case '+': result=(int) Double.parseDouble(args[0]) +
(int) Double.parseDouble(args[2]);
break;
case '-': result=Integer.parseInt(args[0]) -
Integer.parseInt(args[2]);
break;
case '*': result=(int)Double.parseDouble(args[0])*
(int) Double.parseDouble(args[2]);
break;
case '/': result=Integer.parseInt(args[0]) /
Integer.parseInt(args[2]);
}

System.out.print(args[0]+ " " +    args[1] + " " + args[2] + " = "+  df.format(result)+"\n\n");

}
}

class InvalidDataException1 extends Exception { String mistake;
public  InvalidDataException1(String err)
{
super(err);     // call super class constructor
mistake = err;  // save message
}

public String toString(){
return mistake;
}
}
``````

Output:
``````One of the operands wrong :  4/5 7
Usage: java Calculator operand1 operator operand2
``````
0

LVL 47

Expert Comment

ID: 36934793

Change is in this part

``````   try {
isNumeric(ss[0]);
isNumeric(ss[2]);
}catch(NumberFormatException ex){
throw new InvalidDataException1 ("One of the operands wrong :  " + ss[0] + " " + ss[2]);
}
``````
0

LVL 34

Author Comment

ID: 36934800
I guess we couldn't say:

Wrong Input: 4/5

you are giving both numbers. it will be nice to give only the bad input.

any hope for it?
0

LVL 47

Expert Comment

ID: 36934808

You can split this in two:
`````` try {
isNumeric(ss[0]);
isNumeric(ss[2]);
}catch(NumberFormatException ex){
throw new InvalidDataException1 ("One of the operands wrong :  " + ss[0] + " " + ss[2]);
}
``````

``````try {
isNumeric(ss[0]);
}catch(NumberFormatException ex){
throw new InvalidDataException1 ("First operand wrong :  " + ss[0]);
}

try{
isNumeric(ss[2]);
}catch(NumberFormatException ex){
throw new InvalidDataException1 ("Second operand wrong :  " + ss[2]);
}
``````

0

LVL 34

Author Comment

ID: 36934818
Thanks. also

First operand wrong :  3/4
Usage: java Calculator operand1 operator operand2

The second line is not necessary. Can we get rid of it when we get

First operand wrong :  3/4
0

LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 36934829

remove this
``````                     System.out.println(
"Usage: java Calculator operand1 operator operand2\n");
``````

from:

``````  }catch(InvalidDataException1 ex){
System.out.println( ex.toString());
//  ex.printStackTrace();
System.out.println(
"Usage: java Calculator operand1 operator operand2\n");
System.exit(0);
}
``````
0

LVL 34

Author Comment

ID: 36934853
I will try it now.

I just added a new question at:
http://www.experts-exchange.com/Programming/Languages/Java/Q_27386597.html
0

LVL 34

Author Closing Comment

ID: 36934861
Thank you
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how â€¦
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different dâ€¦
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importingâ€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month15 days, 5 hours left to enroll