Expanding exception handling

I added InvalidDataException1 to this line:
   
public static int findSumIn(String s)
           throws NumberFormatException, WrongNumberOfInputsException, InvalidDataException1 {

In order to be able to add two new try{} to check on the validity of input data.

Operation: User to enter 4 5 to get 9 (no operator necessary). If

a 6   <-- invalid data, then prompt the user (after giving error message) to try it again.

The code below works in NetBeans but it gives 3 compile errors (complaining about InvalidDataException1).

Could you see where the problem is?

Thank you.
import java.util.Scanner;

public class Exercise13_2 {

   static int number1;
   static int number2;

   public static void main(String[] args) {

        boolean terminate=false;

        while(terminate==false) {
            
            Scanner input=new Scanner(System.in);
            System.out.println("Enter two integers (ex. 2 4) to get the sum 6:");
            String str = input.nextLine();
            
            try{
                Integer result=findSumIn(str);
                System.out.println("Sum of two integers is: " + result +"\n");
                terminate=true;
            } catch(Exception ex){
                System.out.println(ex);
            }
        }
   }
   
   public static int findSumIn(String s) 
           throws NumberFormatException, WrongNumberOfInputsException, InvalidDataException1 {

        String[] inputs = s.split(" ");

        try {
            if(inputs.length!=2)
                throw new WrongNumberOfInputsException("You need to enter two args\n");       
        }
        catch(NumberFormatException ex){
            throw ex;
        }
        
        try {
            number1=Integer.parseInt(inputs[0]);      
        }catch(NumberFormatException ex){
             throw new InvalidDataException1 ("Wrong Input:  " + inputs[0]);        
        }

        try {
            number2=Integer.parseInt(inputs[1]);      
        }catch(NumberFormatException ex){
             throw new InvalidDataException1 ("Wrong Input:  " + inputs[1]);        
        }
        
        return number1+number2;
   }
    
}

class  WrongNumberOfInputsException extends Exception {
    private String s;
    public   WrongNumberOfInputsException(String s){
        this.s = s;
    }

    public String toString() {return s;}

}

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
I see no compilation errors

Perhaps you have class
WrongNumberOfInputsException
already in the same Project and therefore you see the conflict

0
for_yanCommented:
I have no problem with InvalidDataException1

I think you have some issue with duplicate class in the project, because you had the same classes within another file
0
Gurvinder Pal SinghCommented:
<<The code below works in NetBeans but it gives 3 compile errors (complaining about InvalidDataException1).>>
Is InvalidDataException1 class there in the same package? and compiled successfully?
If not, please import that class
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I copied it to desktop and tried to compile it. see the image:
Exercise13-2CompileError.png
0
Gurvinder Pal SinghCommented:
Then the reason is one of those mentioned in my previous post only.

0
for_yanCommented:
You probably have it named InvalidDataException
Check that - I mentioned that I renamed it
0
for_yanCommented:

create package exercise132 in NetBeans
and put the code below into that package - then
it should work and should not clash with your previous classes

package exercise132;

import java.util.Scanner;

public class Exercise13_2 {

   static int number1;
   static int number2;

   public static void main(String[] args) {

        boolean terminate=false;

        while(terminate==false) {
            
            Scanner input=new Scanner(System.in);
            System.out.println("Enter two integers (ex. 2 4) to get the sum 6:");
            String str = input.nextLine();
            
            try{
                Integer result=findSumIn(str);
                System.out.println("Sum of two integers is: " + result +"\n");
                terminate=true;
            } catch(Exception ex){
                System.out.println(ex);
            }
        }
   }
   
   public static int findSumIn(String s) 
           throws NumberFormatException, WrongNumberOfInputsException, InvalidDataException1 {

        String[] inputs = s.split(" ");

        try {
            if(inputs.length!=2)
                throw new WrongNumberOfInputsException("You need to enter two args\n");       
        }
        catch(NumberFormatException ex){
            throw ex;
        }
        
        try {
            number1=Integer.parseInt(inputs[0]);      
        }catch(NumberFormatException ex){
             throw new InvalidDataException1 ("Wrong Input:  " + inputs[0]);        
        }

        try {
            number2=Integer.parseInt(inputs[1]);      
        }catch(NumberFormatException ex){
             throw new InvalidDataException1 ("Wrong Input:  " + inputs[1]);        
        }
        
        return number1+number2;
   }
    
}

class  WrongNumberOfInputsException extends Exception {
    private String s;
    public   WrongNumberOfInputsException(String s){
        this.s = s;
    }

    public String toString() {return s;}

}

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;
    }
}

Open in new window

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
re:> Then the reason is one of those mentioned in my previous post only.

-It works ok in Netbeans

- When I changed (removed some parts) as shown below it compile. but of course the input integer are not being changed.

The whole idea was to have 3 try{} instead of one for detail exception handling purpose.
public static int findSumIn(String s) 
           throws NumberFormatException, WrongNumberOfInputsException, InvalidDataException1 {

        String[] inputs = s.split(" ");

        try {
            if(inputs.length!=2)
                throw new WrongNumberOfInputsException("You need to enter two args\n");       
        }
        catch(NumberFormatException ex){
            throw ex;
        }
        
        try {
            number1=Integer.parseInt(inputs[0]);      
        }catch(NumberFormatException ex){
             throw new InvalidDataException1 ("Wrong Input:  " + inputs[0]);        
        }

        try {
            number2=Integer.parseInt(inputs[1]);      
        }catch(NumberFormatException ex){
             throw new InvalidDataException1 ("Wrong Input:  " + inputs[1]);        
        }
        
        return number1+number2;
   }

Open in new window

public static int findSumIn(String s) 
           throws NumberFormatException, WrongNumberOfInputsException {

        String[] inputs = s.split(" ");

        try {
            if(inputs.length!=2)
                throw new WrongNumberOfInputsException("You need to enter two args\n");       
        }
        catch(NumberFormatException ex){
            throw ex;
        }
               
        return number1+number2;
   }

Open in new window

0
for_yanCommented:
No sorry, i pasted your old exception class. just a moment
0
for_yanCommented:
Paste the whole class as you are using it now and explain what you  want to see
0
Gurvinder Pal SinghCommented:
<<-It works ok in Netbeans>>
You can always specify classpath and add dependent projects in netbeans, which you may forget to do in command line

Please check again
0
for_yanCommented:
You probably should name exception classes with the same ending as your execrcise number and class name -
and alwys keep them in the same file - otherwise you'll have these issues with clashes
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
One or more projects were compiled with errors.

With InvalidDataException1 underlined red in 3 places
0
for_yanCommented:
Hover over the red - it will tell you that it cannot find symbol or that it has duplicate symbol
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
re:> You can always specify classpath and add dependent projects in netbeans, which you may forget to do in command line

I am not sure if it refers to an outside class. I am not sure.
0
for_yanCommented:
Do you have each exercise in the separate project ?
0
for_yanCommented:
No don't go away from Netbeans to command line and you don't have anything to specify in dependencies - all of them are in your code
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I am lost. Not sure. I will check to see if it have it in netbean folder or in another project under a different project.
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
It has to be something with the same name, right?
0
stachenovCommented:
Compiles fine for me from command line, given that InvalidDataException1 is declared in the file named "InvalidDataException1.java" in the same directory as "Exercise13_2.java", so please double-check that.

Once you got that working from command line, you'd better re-create your Netbeans project from scratch, putting those two files in a separate directory and using the "Create new project from existing sources" option, since it could be some sort of Netbeans bug. I remember it not finding my classes before.
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
in the process of house cleaning I have deleted couple of files. Most likely the damage is done then.

- I can get it back from windows 7 trash beans.
- most likely it is somewhere i=posted at EE oct 3 or 4th
- It is in a netbeans folder
0
for_yanCommented:
Create new project in Netbeans.
Create new class Exercise13_2 in defaiult package
and paste the whole code below into that
class - including two Exceptions non=public classes at the bottom of the code below
Then compile and run - it should work (see output below):


import java.util.Scanner;

public class Exercise13_2 {

   static int number1;
   static int number2;

   public static void main(String[] args) {

        boolean terminate=false;

        while(terminate==false) {

            Scanner input=new Scanner(System.in);
            System.out.println("Enter two integers (ex. 2 4) to get the sum 6:");
            String str = input.nextLine();

            try{
                Integer result=findSumIn(str);
                System.out.println("Sum of two integers is: " + result +"\n");
                terminate=true;
            } catch(Exception ex){
                System.out.println(ex);
            }
        }
   }

   public static int findSumIn(String s)
           throws NumberFormatException, WrongNumberOfInputsException, InvalidDataException1 {

        String[] inputs = s.split(" ");

        try {
            if(inputs.length!=2)
                throw new WrongNumberOfInputsException("You need to enter two args\n");
        }
        catch(NumberFormatException ex){
            throw ex;
        }

        try {
            number1=Integer.parseInt(inputs[0]);
        }catch(NumberFormatException ex){
             throw new InvalidDataException1 ("Wrong Input:  " + inputs[0]);
        }

        try {
            number2=Integer.parseInt(inputs[1]);
        }catch(NumberFormatException ex){
             throw new InvalidDataException1 ("Wrong Input:  " + inputs[1]);
        }

        return number1+number2;
   }

}

class  WrongNumberOfInputsException extends Exception {
    private String s;
    public   WrongNumberOfInputsException(String s){
        this.s = s;
    }

    public String toString() {return s;}

}

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;
    }
}

Open in new window



Enter two integers (ex. 2 4) to get the sum 6:
a 4
Wrong Input:  a
Enter two integers (ex. 2 4) to get the sum 6:
4/5 3
Wrong Input:  4/5
Enter two integers (ex. 2 4) to get the sum 6:
3 6
Sum of two integers is: 9

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I found the file. I am compiling again.
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
ok, I will do your solution first
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
error:

class InvalidDataException1 extends Exception { String mistake;
    public  InvalidDataException1(String err)

duplicate class
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
But it should be okay outside netbeans, I will try now
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I work thanks.
0
for_yanCommented:
This code should work both in Netbeans and outside - if you put the whole code in one class


import java.util.Scanner;

public class Exercise13_2 {

   static int number1;
   static int number2;

   public static void main(String[] args) {

        boolean terminate=false;

        while(terminate==false) {

            Scanner input=new Scanner(System.in);
            System.out.println("Enter two integers (ex. 2 4) to get the sum 6:");
            String str = input.nextLine();

            try{
                Integer result=findSumIn(str);
                System.out.println("Sum of two integers is: " + result +"\n");
                terminate=true;
            } catch(Exception ex){
                System.out.println(ex);
            }
        }
   }

   public static int findSumIn(String s)
           throws NumberFormatException, WrongNumberOfInputsException132, InvalidDataException132 {

        String[] inputs = s.split(" ");

        try {
            if(inputs.length!=2)
                throw new WrongNumberOfInputsException132("You need to enter two args\n");
        }
        catch(NumberFormatException ex){
            throw ex;
        }

        try {
            number1=Integer.parseInt(inputs[0]);
        }catch(NumberFormatException ex){
             throw new InvalidDataException132 ("Wrong Input:  " + inputs[0]);
        }

        try {
            number2=Integer.parseInt(inputs[1]);
        }catch(NumberFormatException ex){
             throw new InvalidDataException132 ("Wrong Input:  " + inputs[1]);
        }

        return number1+number2;
   }

}

class  WrongNumberOfInputsException132 extends Exception {
    private String s;
    public   WrongNumberOfInputsException132(String s){
        this.s = s;
    }

    public String toString() {return s;}

}

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

    public String toString(){
        return mistake;
    }
}

Open in new window

0
Gurvinder Pal SinghCommented:
so it means there is some dependency which you have specified in Netbeans which you didn't specified on command line, right?
0
Gurvinder Pal SinghCommented:
glad that it worked for you.
@for_yan: you have a patience of a Saint. Good work
0
for_yanCommented:
Do it better this way - keep your Exceptions with the main class and give them
modified names like inmy last code - you'll have no clashes.
0
for_yanCommented:
gurvinder, thanks  for the kind words.
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Yes that is what I should do. Now I know better.

I have damaged lots of file this way. I have to go over them on the weekend,

Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.