0

I am having a problem with try and catch. My program is to insert three different strings name, address and phone number then I convert these three in to a single String using toString method.

I have problem with exception handling whenever I write a wrong choice (String or other data type) then catch works infinity times.

import java.util.ArrayList;
import java.util.Scanner;

public class mainClass {

  public static void main(String[] args) {
    Scanner input= new Scanner(System.in);
    ArrayList<String> arraylist= new ArrayList<String>();
    CreateFormat FormatObject = new CreateFormat();

    int choice;
    String phoneNumber;
    String name,address;
    String format="Empty";
    int x=1;
    int flag=0;
    do
    {
    try

    {   
    System.out.println("Enter your choice");
    System.out.printf("1:Enter new data\n2:Display data");
    choice=input.nextInt();
    switch (choice)
    {
    case 1:
    {
        System.out.println("Enter name  ");
        name=input.next();
        System.out.println("Enter phone number");
        phoneNumber=input.next();
        System.out.println("Enter address");
        address=input.next();
        format=FormatObject.toString(phoneNumber, name, address);
        arraylist.add(format);
        flag++;


    }
        break;
    case 2:
    {
        System.out.println("Name   Phone number   Address");
        System.out.println();
        for(int i=0;i<flag;i++)
        {   
        System.out.println(arraylist.get(i));

        }
    }
        break;
    }

}

 catch(Exception InputMismatchException){
System.out.println("Enter right choice");`
   }while(x==1);
}
}


//The format class ...//returns format for string

2 Answers 2

6

Your try and catch are not related to a loop, nor to your problem.

while(x==1)

is what you test on, yet you never change the value of x, so it will always remain 1, and thus the above check will always return true.

Sign up to request clarification or add additional context in comments.

9 Comments

how can i escape catch ? and then return to try...in the while loop
The problem you have is not to return to try (because that's what your loop does), the problem is that you never leave that loop of yours. There must be a state where x==1 is false.
@SarmadAijaz since you are the only one who has access to it's requirements: probably, but not guaranteed correctly
@Stultuske is it wise to update X value in catch block and have check based on it ?
@unknown at the moment, no matter what he does, he has an infinite loop. the x should be updated as last element of the try block, not in the catch, since when an exception occurs, I assume he wants to repeat the steps
|
0

I think I now know what your problem actually is.

Simply adding input.nextLine() at the very beginning at your code will stop the input running havoc.

boolean wrongInput = false;
do {
   try {
       if (wrongInput) {
          input.nextLine();
          wrongInput = false;   
       }
       System.out.println("Enter your choice");
       [...]
   } catch (...) {
       wrongInput = true;
   }

should do the trick. However, please note that I noticed two errors in your program (which might be because I do not have the CreateFormat class of yours), (a) I cannot add a number to the address and (b) there is no option to stop the loop (which I strongly recommend - where you simply set x = -1 or something similar, better use a boolean to end the loop though).

2 Comments

x==1 => this is a boolean. the naming convention might be clearer, but for all we know, there are different values to be taken into account for x later on, not just true or false
i did integer.parsInt(input.next());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.