For loop resets after exception


Jasfer

Currently, I am doing a multiple choice quiz. But I am stuck with this problem. After the exception, my for loop resets and basically returns 0. Is there any other way to solve this problem? Or is my code logically wrong? The for loop resets whenever an exception is thrown. For example, I have an exception on question 5 and the for loop returns to 0.

do{

  try {

  Scanner A = new Scanner (System.in);

  for (int x = 0; x < Questions.length;x++){

      loop = x;
      System.out.println(Questions[x]);
      System.out.println(Choices[x]);

      System.out.println("");
      System.out.print("Answer: ");

      UAnswer = A.nextLine();


      if (UAnswer.equalsIgnoreCase("A") || UAnswer.equalsIgnoreCase("B") || UAnswer.equalsIgnoreCase("C")){

          if (UAnswer.equalsIgnoreCase(AnswerKey[x])){
          System.out.println("");
          System.out.println("Correct Answer!\n");
          Score++;
          }
          else {
              System.out.println("");
              System.out.println("Wrong! The correct answer is: " + AnswerKey[x] + "\n");

          }

      }
      else if (UAnswer.equalsIgnoreCase("")){
          throw new NullPointerException();
        }
      else {
          throw new InputMismatchException(); 
        }

      }
  }
  catch(InputMismatchException ime){
  System.out.println("");
    System.out.println("Invalid input! Please type A,B,C!\n");

      }


  catch(NullPointerException b){
  System.out.println("");
  System.out.println("You did not input any answer.\n");

      }

    }while(loop != 9);
Justin

When you throw an exception, you're back at the beginning of the do while loop, because that's where your try block begins. If you don't want to reset the loop on exception, put a try catch block inside the for loop.

Related


For loop resets after exception

Jasfer Currently, I am doing a multiple choice quiz. But I am stuck with this problem. After the exception, my for loop resets and basically returns 0. Is there any other way to solve this problem? Or is my code logically wrong? The for loop resets whenever an

How to continue the loop after an exception?

Nanoni I have a code where im going through a list of hosts and appending connections to the list of connections, if there is a connection error I want to skip the error and move on to the next host in the list of hosts. Here is what I have now: def do_connect

Continue loop after Java exception

im_mangesh: Here is my code: if (Recipients_To != null) { for (int i = 0; i < Recipients_To.length; i++) { message.setRecipients(Message.RecipientType.TO, Recipients_To[i].toString()); Transport.send(message); } } I have a list of 500+

Continue the loop after catching the exception

murderous I am going through reading large files (2Gb) csv.DictReader(this is important). Somewhere deep in the file, there is a problem with the encoding and I get UnicodeDecodeError. Looking at the error message, I see that the error is raised implicitly in

How to continue the loop after an exception?

Nanoni I have a code where im going through a list of hosts and appending connections to the list of connections, if there is a connection error I want to skip the error and move on to the next host in the list of hosts. Here is what I have now: def do_connect

How to continue the loop after an exception?

Nanoni I have a code where im going through a list of hosts and appending connections to the list of connections, if there is a connection error I want to skip the error and move on to the next host in the list of hosts. Here is what I have now: def do_connect

Continue loop after exception in Python

Loewe 8 So first I saw similar questions, but nothing worked / didn't apply to my problem. I am writing a program that is including a lot of search queries to be searched on Youtube in a Text file. The program iterates through the text file line by line. But t

Continue the loop after catching the exception

murderous I am going through reading large files (2Gb) csv.DictReader(this is important). Somewhere deep in the file, there is a problem with the encoding and I get UnicodeDecodeError. Looking at the error message, I see that the error is raised implicitly in

Continue while loop after exception

Dorothy I have this code. I want to go back to the beginning of the loop and ask for user input again. However, it always loops and keeps asking for input. What's wrong with my code? thanks while(true){ ... try { int choice = input.nextInt();

Continue the loop after catching the exception

murderous I am going through reading large files (2Gb) csv.DictReader(this is important). Somewhere deep in the file, there is a problem with the encoding and I get UnicodeDecodeError. Looking at the error message, I see that the error is raised implicitly in

Continue the loop after catching the exception

murderous I am going through reading large files (2Gb) csv.DictReader(this is important). Somewhere deep in the file, there is a problem with the encoding and I get UnicodeDecodeError. Looking at the error message, I see that the error is raised implicitly in

Continue loop after Java exception

im_mangesh: Here is my code: if (Recipients_To != null) { for (int i = 0; i < Recipients_To.length; i++) { message.setRecipients(Message.RecipientType.TO, Recipients_To[i].toString()); Transport.send(message); } } I have a list of 500+

How to continue the loop after an exception?

Nanoni I have a code where im going through a list of hosts and appending connections to the list of connections, if there is a connection error I want to skip the error and move on to the next host in the list of hosts. Here is what I have now: def do_connect

How to continue the loop after an exception?

Nanoni I have a code where im going through a list of hosts and appending connections to the list of connections, if there is a connection error I want to skip the error and move on to the next host in the list of hosts. Here is what I have now: def do_connect

Continue the loop after catching the exception

murderous I am going through reading large files (2Gb) csv.DictReader(this is important). Somewhere deep in the file, there is a problem with the encoding and I get UnicodeDecodeError. Looking at the error message, I see that the error is raised implicitly in

Continue loop after exception in Python

Loewe 8 So first I saw similar questions, but nothing worked / didn't apply to my problem. I am writing a program that is including a lot of search queries to be searched on Youtube in a Text file. The program iterates through the text file line by line. But t

Cakephp throws exception and resets property

User 3803241 I have a component, and tested it for performance. In the controller I do this: $this->ApiRequest->test = "gg"; throw new NotFoundException; After executing the ExceptionRenderer, I try to read the property in the component's beforeRender method:

Cakephp throws exception and resets property

User 3803241 I have a component, and tested it for performance. In the controller I do this: $this->ApiRequest->test = "gg"; throw new NotFoundException; After executing the ExceptionRenderer, I try to read the property in the component's beforeRender method:

Python: Index in for loop resets for no reason

Nixter First of all, I want to apologize for my English because it's not my native language. I have a really crazy problem with the following code: linecounter = [] for i in range(20): linecounter.append("Color "+str(i)+"\n") for line in linecounter:

Python: Index in for loop resets for no reason

Nixter First of all, I want to apologize for my English because it's not my native language. I have a really crazy problem with the following code: linecounter = [] for i in range(20): linecounter.append("Color "+str(i)+"\n") for line in linecounter:

DataGridComboBoxColumn value resets after selection

Metronome I have the following DataGrid control which should probably display: checkbox (select), combo box (select action), column 1 (from database), column 2 (from database) <DataGrid x:Name="dgDatabase" HorizontalAlignment="Left" Margin="10,10,0,0" Vertical

MKMapView resets after changing view

aluminum MKMapViewI'm having issues with constant resets after changing the current view to another view and back to the map again . In fact, the first time everything was fine and the map was correctly centered on the user location, but the second time it did

Laravel session resets after reload

random My Laravel session doesn't seem to be working. When I reload the page, all session data seems to be lost. IE: CSRF token session::put('key', 'value'); session::get('key'); CSRF token Login form: Session::token() => Cr3TwtQOWg6O2zV8rfksTQfqwEb3NxXjmIiA54

ScrollView contentOffset resets after pushViewController

Renan Kosicki I have a problem with using ScrollView, my scrollView has width 640, from 0 to 320 is mapView and from 320 to 640 is tableView. I have a segmentedButton with 2 options to toggle the contentOffset in x from 0 to 320. The problem is that if I switc

ScrollView contentOffset resets after pushViewController

Renan Kosicki I have a problem with using ScrollView, my scrollView has width 640, from 0 to 320 is mapView and from 320 to 640 is tableView. I have a segmentedButton with 2 options to toggle the contentOffset in x from 0 to 320. The problem is that if I switc

Custom moreNavigationController resets after rotation

McCawan I use UITabBarController for more than 5 projects. I customized the moreNavigationController to remove the navigation bar and make it dim. But when I rotate the device to landscape and it comes to portrait again, it shows the navbar on top, white again

Laravel session resets after reload

random My Laravel session doesn't seem to be working. When I reload the page, all session data seems to be lost. IE: CSRF token session::put('key', 'value'); session::get('key'); CSRF token Login form: Session::token() => Cr3TwtQOWg6O2zV8rfksTQfqwEb3NxXjmIiA54