简体   繁体   中英

If an invalid input is entered during an iteration of a loop, how do I get the loop to start again at its current iteration

I am new to programming and am working on a Java assignment where a user names a file. The file is then passed to a method for the user to write in test grades, then passed to another method to read and display the test grades with the class average.

I am currently working on the method to write the students grades to the file which I have successfully done, but I need to implement error checking if a non numeric value is entered. I used a while loop with try and catch for InputMismatchException, the loop works, sometimes. If an invalid input is entered on a the loops first iteration it will catch it and reiterate, however if an invalid input is entered on any other iteration besides the first it will catch the InputMismatchException, but it will also break out of the loop and continue to the next method. How would I get the loop to start at the iteration of the loop that was invalid. I would appreciate any help, and I am sorry if my code is poorly written as I am very new to programming.

Here is the method I am working on:

 public static void inputScores(String newFile) 
{
    Scanner scan = new Scanner(System.in);
    Formatter write;

    try
    {
        write = new Formatter(newFile +".txt");

        double score = 0.0;
        int count = 1;
        boolean again = false;          


        while(!again){
            try
            {
                while(score >= 0)
                {
                System.out.print("Please enter student " + count + "'s test score, input -1 to quit:\n>");
                score = scan.nextDouble();
                again = true;
                count++;

                   if(score >= 0)
                   {
                       write.format("%.2f%n", score);
                   }
                   else if(score <= -1)
                   {
                       break;
                   }                                    
                }                    
            }
            catch(InputMismatchException ex){
                    System.out.println("Invalid input, Student's scores must be a number");
                    scan.next();
                }       
        }
         write.close();
    }

    catch(FileNotFoundException e)
    {
        System.out.println(e.getMessage());
    }      
}  

Try this code. Hope this will fulfill your requirement.

    public static void inputScores(String newFile) {
        Scanner scan = new Scanner(System.in);
        Formatter write=null;
        try {
            write = new Formatter(newFile + ".txt");
            double score = 0.0;
            int count = 1;
            while(true) {
                try {
                System.out.println("Please enter student " + count + "'s test score, input -1 to quit:\n>");
                score = scan.nextDouble();
                count++;
                if(score>=0)
                    write.format("%.2f%n", score);
                else
                    break;
                }catch (InputMismatchException e) {
                    System.err.println("Invalid input, Student's scores must be a number");
                    scan.next();
                    System.out.println();
                }
            }
            write.close();
            scan.close();
        }
        catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM