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());
java while-loop iteration try-catch inputmismatchexception
add a comment |
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());
java while-loop iteration try-catch inputmismatchexception
catch(InputMismatchException ex) again = false;
?
– fantaghirocco
Nov 14 '18 at 17:20
add a comment |
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());
java while-loop iteration try-catch inputmismatchexception
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());
java while-loop iteration try-catch inputmismatchexception
java while-loop iteration try-catch inputmismatchexception
asked Nov 14 '18 at 16:48
EiffelFlowersEiffelFlowers
31
31
catch(InputMismatchException ex) again = false;
?
– fantaghirocco
Nov 14 '18 at 17:20
add a comment |
catch(InputMismatchException ex) again = false;
?
– fantaghirocco
Nov 14 '18 at 17:20
catch(InputMismatchException ex) again = false;
?– fantaghirocco
Nov 14 '18 at 17:20
catch(InputMismatchException ex) again = false;
?– fantaghirocco
Nov 14 '18 at 17:20
add a comment |
1 Answer
1
active
oldest
votes
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());
Yes! This works. Thank you so much, you are very much appreciated!
– EiffelFlowers
Nov 14 '18 at 17:34
@EiffelFlowers If you got your answer then please vote up and marked as answered or if you don't have sufficient reputation then at least like it.
– Altaf
Nov 14 '18 at 17:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305098%2fif-an-invalid-input-is-entered-during-an-iteration-of-a-loop-how-do-i-get-the-l%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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());
Yes! This works. Thank you so much, you are very much appreciated!
– EiffelFlowers
Nov 14 '18 at 17:34
@EiffelFlowers If you got your answer then please vote up and marked as answered or if you don't have sufficient reputation then at least like it.
– Altaf
Nov 14 '18 at 17:43
add a comment |
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());
Yes! This works. Thank you so much, you are very much appreciated!
– EiffelFlowers
Nov 14 '18 at 17:34
@EiffelFlowers If you got your answer then please vote up and marked as answered or if you don't have sufficient reputation then at least like it.
– Altaf
Nov 14 '18 at 17:43
add a comment |
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());
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());
answered Nov 14 '18 at 17:20
AltafAltaf
3610
3610
Yes! This works. Thank you so much, you are very much appreciated!
– EiffelFlowers
Nov 14 '18 at 17:34
@EiffelFlowers If you got your answer then please vote up and marked as answered or if you don't have sufficient reputation then at least like it.
– Altaf
Nov 14 '18 at 17:43
add a comment |
Yes! This works. Thank you so much, you are very much appreciated!
– EiffelFlowers
Nov 14 '18 at 17:34
@EiffelFlowers If you got your answer then please vote up and marked as answered or if you don't have sufficient reputation then at least like it.
– Altaf
Nov 14 '18 at 17:43
Yes! This works. Thank you so much, you are very much appreciated!
– EiffelFlowers
Nov 14 '18 at 17:34
Yes! This works. Thank you so much, you are very much appreciated!
– EiffelFlowers
Nov 14 '18 at 17:34
@EiffelFlowers If you got your answer then please vote up and marked as answered or if you don't have sufficient reputation then at least like it.
– Altaf
Nov 14 '18 at 17:43
@EiffelFlowers If you got your answer then please vote up and marked as answered or if you don't have sufficient reputation then at least like it.
– Altaf
Nov 14 '18 at 17:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305098%2fif-an-invalid-input-is-entered-during-an-iteration-of-a-loop-how-do-i-get-the-l%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
catch(InputMismatchException ex) again = false;
?– fantaghirocco
Nov 14 '18 at 17:20