Write a program that will give the use prompt to enter two doubles value
Professor requires us to write a program that will give the user prompt to enter two float (or double) values. If the values inputted are correct then display the inputted two values. If user enters characters instead of numbers or if they enter invalid numbers then the program will display the error message and ask the user to re-enter the correct values again. It only exits when the correct input is received and displayed.
However, I wrote a program that will only work if the user input the two right doubles. Can someone helps me to change the line about catching errors? Thanks.
import java.util.Scanner;
public class FiveSecond {
static void printMenu() {
System.out.println("Welcome to get two doubles program:");
}
public static void main(String args) {
Scanner scan = new Scanner(System.in);
boolean valid = false;
double first = 0;
double second = 0;
printMenu();
while(!valid) {
System.out.print("Enter two doubles, seperate by space ");
try {
first = Double.parseDouble(scan.next());
second = Double.parseDouble(scan.next());
} catch (NumberFormatException e) {
System.out.println("Try again");
}
valid = true;
}
System.out.println("You entered valid choice: " + first + " " +second);
System.out.println("Thank you for giving your choice.");
scan.close();
}
}
java
add a comment |
Professor requires us to write a program that will give the user prompt to enter two float (or double) values. If the values inputted are correct then display the inputted two values. If user enters characters instead of numbers or if they enter invalid numbers then the program will display the error message and ask the user to re-enter the correct values again. It only exits when the correct input is received and displayed.
However, I wrote a program that will only work if the user input the two right doubles. Can someone helps me to change the line about catching errors? Thanks.
import java.util.Scanner;
public class FiveSecond {
static void printMenu() {
System.out.println("Welcome to get two doubles program:");
}
public static void main(String args) {
Scanner scan = new Scanner(System.in);
boolean valid = false;
double first = 0;
double second = 0;
printMenu();
while(!valid) {
System.out.print("Enter two doubles, seperate by space ");
try {
first = Double.parseDouble(scan.next());
second = Double.parseDouble(scan.next());
} catch (NumberFormatException e) {
System.out.println("Try again");
}
valid = true;
}
System.out.println("You entered valid choice: " + first + " " +second);
System.out.println("Thank you for giving your choice.");
scan.close();
}
}
java
2
Try settingvalid
totrue
inside thetry
block. As it is, it'll betrue
regardless of whether an exception was thrown.
– jsheeran
Nov 14 '18 at 13:45
@jsheeran I have tried your solution and it really helps me out. This drives me crazy all night, thank you very much.
– Z. Zhang
Nov 14 '18 at 13:52
You need to learn how to debug your code. In fact, universities should teach how to debug code before teaching how to write code.
– Max Vollmer
Nov 14 '18 at 13:52
@MaxVollmer Hi, max. Do you know somewhere to learn how to debug my code? I'm not computer science major during the college time. However, I want to get a master degree of the data science. Most programs suggest the applicants has background in Java, that's why I'm start learning write code with the UCSC extension.
– Z. Zhang
Nov 14 '18 at 13:59
Here is a good start: What is a debugger and how can it help me diagnose problems?
– Max Vollmer
Nov 14 '18 at 14:00
add a comment |
Professor requires us to write a program that will give the user prompt to enter two float (or double) values. If the values inputted are correct then display the inputted two values. If user enters characters instead of numbers or if they enter invalid numbers then the program will display the error message and ask the user to re-enter the correct values again. It only exits when the correct input is received and displayed.
However, I wrote a program that will only work if the user input the two right doubles. Can someone helps me to change the line about catching errors? Thanks.
import java.util.Scanner;
public class FiveSecond {
static void printMenu() {
System.out.println("Welcome to get two doubles program:");
}
public static void main(String args) {
Scanner scan = new Scanner(System.in);
boolean valid = false;
double first = 0;
double second = 0;
printMenu();
while(!valid) {
System.out.print("Enter two doubles, seperate by space ");
try {
first = Double.parseDouble(scan.next());
second = Double.parseDouble(scan.next());
} catch (NumberFormatException e) {
System.out.println("Try again");
}
valid = true;
}
System.out.println("You entered valid choice: " + first + " " +second);
System.out.println("Thank you for giving your choice.");
scan.close();
}
}
java
Professor requires us to write a program that will give the user prompt to enter two float (or double) values. If the values inputted are correct then display the inputted two values. If user enters characters instead of numbers or if they enter invalid numbers then the program will display the error message and ask the user to re-enter the correct values again. It only exits when the correct input is received and displayed.
However, I wrote a program that will only work if the user input the two right doubles. Can someone helps me to change the line about catching errors? Thanks.
import java.util.Scanner;
public class FiveSecond {
static void printMenu() {
System.out.println("Welcome to get two doubles program:");
}
public static void main(String args) {
Scanner scan = new Scanner(System.in);
boolean valid = false;
double first = 0;
double second = 0;
printMenu();
while(!valid) {
System.out.print("Enter two doubles, seperate by space ");
try {
first = Double.parseDouble(scan.next());
second = Double.parseDouble(scan.next());
} catch (NumberFormatException e) {
System.out.println("Try again");
}
valid = true;
}
System.out.println("You entered valid choice: " + first + " " +second);
System.out.println("Thank you for giving your choice.");
scan.close();
}
}
java
java
edited Nov 14 '18 at 14:02
deHaar
2,45451628
2,45451628
asked Nov 14 '18 at 13:43
Z. ZhangZ. Zhang
112
112
2
Try settingvalid
totrue
inside thetry
block. As it is, it'll betrue
regardless of whether an exception was thrown.
– jsheeran
Nov 14 '18 at 13:45
@jsheeran I have tried your solution and it really helps me out. This drives me crazy all night, thank you very much.
– Z. Zhang
Nov 14 '18 at 13:52
You need to learn how to debug your code. In fact, universities should teach how to debug code before teaching how to write code.
– Max Vollmer
Nov 14 '18 at 13:52
@MaxVollmer Hi, max. Do you know somewhere to learn how to debug my code? I'm not computer science major during the college time. However, I want to get a master degree of the data science. Most programs suggest the applicants has background in Java, that's why I'm start learning write code with the UCSC extension.
– Z. Zhang
Nov 14 '18 at 13:59
Here is a good start: What is a debugger and how can it help me diagnose problems?
– Max Vollmer
Nov 14 '18 at 14:00
add a comment |
2
Try settingvalid
totrue
inside thetry
block. As it is, it'll betrue
regardless of whether an exception was thrown.
– jsheeran
Nov 14 '18 at 13:45
@jsheeran I have tried your solution and it really helps me out. This drives me crazy all night, thank you very much.
– Z. Zhang
Nov 14 '18 at 13:52
You need to learn how to debug your code. In fact, universities should teach how to debug code before teaching how to write code.
– Max Vollmer
Nov 14 '18 at 13:52
@MaxVollmer Hi, max. Do you know somewhere to learn how to debug my code? I'm not computer science major during the college time. However, I want to get a master degree of the data science. Most programs suggest the applicants has background in Java, that's why I'm start learning write code with the UCSC extension.
– Z. Zhang
Nov 14 '18 at 13:59
Here is a good start: What is a debugger and how can it help me diagnose problems?
– Max Vollmer
Nov 14 '18 at 14:00
2
2
Try setting
valid
to true
inside the try
block. As it is, it'll be true
regardless of whether an exception was thrown.– jsheeran
Nov 14 '18 at 13:45
Try setting
valid
to true
inside the try
block. As it is, it'll be true
regardless of whether an exception was thrown.– jsheeran
Nov 14 '18 at 13:45
@jsheeran I have tried your solution and it really helps me out. This drives me crazy all night, thank you very much.
– Z. Zhang
Nov 14 '18 at 13:52
@jsheeran I have tried your solution and it really helps me out. This drives me crazy all night, thank you very much.
– Z. Zhang
Nov 14 '18 at 13:52
You need to learn how to debug your code. In fact, universities should teach how to debug code before teaching how to write code.
– Max Vollmer
Nov 14 '18 at 13:52
You need to learn how to debug your code. In fact, universities should teach how to debug code before teaching how to write code.
– Max Vollmer
Nov 14 '18 at 13:52
@MaxVollmer Hi, max. Do you know somewhere to learn how to debug my code? I'm not computer science major during the college time. However, I want to get a master degree of the data science. Most programs suggest the applicants has background in Java, that's why I'm start learning write code with the UCSC extension.
– Z. Zhang
Nov 14 '18 at 13:59
@MaxVollmer Hi, max. Do you know somewhere to learn how to debug my code? I'm not computer science major during the college time. However, I want to get a master degree of the data science. Most programs suggest the applicants has background in Java, that's why I'm start learning write code with the UCSC extension.
– Z. Zhang
Nov 14 '18 at 13:59
Here is a good start: What is a debugger and how can it help me diagnose problems?
– Max Vollmer
Nov 14 '18 at 14:00
Here is a good start: What is a debugger and how can it help me diagnose problems?
– Max Vollmer
Nov 14 '18 at 14:00
add a comment |
2 Answers
2
active
oldest
votes
Try this:
catch (NumberFormatException e) {
System.out.println("Try again");
continue;
}
add a comment |
In addition to the previous comments, you have to be careful, because the scanner will 'remember' a previously correct double if you don't reset it :
EDITED: Thanks to @Stultuske comment
while (!valid) {
System.out.print("Enter two doubles, seperate by space ");
try {
first = Double.parseDouble(scan.next());
second = Double.parseDouble(scan.next());
valid = true;
}
catch (NumberFormatException e) {
System.out.println("Try again");
scan.nextLine(); // <------- Important line
}
}
seriously? you really should NOT do that. you would actually create a new instance for each iteration? There are better ways to deal with such issues
– Stultuske
Nov 14 '18 at 14:08
@Stultuske : It will create a new instance each time the user enters a incorrect input, not at each iteration. If you know a better way to reset the content of a scanner, I will be glad to learn it.
– Romain Warnan
Nov 14 '18 at 14:21
it will iterate as long as the user inputs invalid data, so yes, it will create a new instance each iteration, which you really shouldn't. just replace that new instantiation with scan.nextLine(); that's all you need to do
– Stultuske
Nov 14 '18 at 14:28
You are right, thank you for explaining. I will edit what I wrote.
– Romain Warnan
Nov 14 '18 at 14:32
@DogEata Hi, thanks for your suggestion, but I didn't find the scanner will remember the previous correct double. e.g. I typed the 3.0 sss in the first time, and 4.0 5.0 in the second time, the scanner will only show 4.0 and 5.0. Somehow, the scanner didn't remember the input even without the line.
– Z. Zhang
Nov 15 '18 at 1:18
|
show 1 more 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%2f53301653%2fwrite-a-program-that-will-give-the-use-prompt-to-enter-two-doubles-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
catch (NumberFormatException e) {
System.out.println("Try again");
continue;
}
add a comment |
Try this:
catch (NumberFormatException e) {
System.out.println("Try again");
continue;
}
add a comment |
Try this:
catch (NumberFormatException e) {
System.out.println("Try again");
continue;
}
Try this:
catch (NumberFormatException e) {
System.out.println("Try again");
continue;
}
answered Nov 14 '18 at 13:59
NickNick
4118
4118
add a comment |
add a comment |
In addition to the previous comments, you have to be careful, because the scanner will 'remember' a previously correct double if you don't reset it :
EDITED: Thanks to @Stultuske comment
while (!valid) {
System.out.print("Enter two doubles, seperate by space ");
try {
first = Double.parseDouble(scan.next());
second = Double.parseDouble(scan.next());
valid = true;
}
catch (NumberFormatException e) {
System.out.println("Try again");
scan.nextLine(); // <------- Important line
}
}
seriously? you really should NOT do that. you would actually create a new instance for each iteration? There are better ways to deal with such issues
– Stultuske
Nov 14 '18 at 14:08
@Stultuske : It will create a new instance each time the user enters a incorrect input, not at each iteration. If you know a better way to reset the content of a scanner, I will be glad to learn it.
– Romain Warnan
Nov 14 '18 at 14:21
it will iterate as long as the user inputs invalid data, so yes, it will create a new instance each iteration, which you really shouldn't. just replace that new instantiation with scan.nextLine(); that's all you need to do
– Stultuske
Nov 14 '18 at 14:28
You are right, thank you for explaining. I will edit what I wrote.
– Romain Warnan
Nov 14 '18 at 14:32
@DogEata Hi, thanks for your suggestion, but I didn't find the scanner will remember the previous correct double. e.g. I typed the 3.0 sss in the first time, and 4.0 5.0 in the second time, the scanner will only show 4.0 and 5.0. Somehow, the scanner didn't remember the input even without the line.
– Z. Zhang
Nov 15 '18 at 1:18
|
show 1 more comment
In addition to the previous comments, you have to be careful, because the scanner will 'remember' a previously correct double if you don't reset it :
EDITED: Thanks to @Stultuske comment
while (!valid) {
System.out.print("Enter two doubles, seperate by space ");
try {
first = Double.parseDouble(scan.next());
second = Double.parseDouble(scan.next());
valid = true;
}
catch (NumberFormatException e) {
System.out.println("Try again");
scan.nextLine(); // <------- Important line
}
}
seriously? you really should NOT do that. you would actually create a new instance for each iteration? There are better ways to deal with such issues
– Stultuske
Nov 14 '18 at 14:08
@Stultuske : It will create a new instance each time the user enters a incorrect input, not at each iteration. If you know a better way to reset the content of a scanner, I will be glad to learn it.
– Romain Warnan
Nov 14 '18 at 14:21
it will iterate as long as the user inputs invalid data, so yes, it will create a new instance each iteration, which you really shouldn't. just replace that new instantiation with scan.nextLine(); that's all you need to do
– Stultuske
Nov 14 '18 at 14:28
You are right, thank you for explaining. I will edit what I wrote.
– Romain Warnan
Nov 14 '18 at 14:32
@DogEata Hi, thanks for your suggestion, but I didn't find the scanner will remember the previous correct double. e.g. I typed the 3.0 sss in the first time, and 4.0 5.0 in the second time, the scanner will only show 4.0 and 5.0. Somehow, the scanner didn't remember the input even without the line.
– Z. Zhang
Nov 15 '18 at 1:18
|
show 1 more comment
In addition to the previous comments, you have to be careful, because the scanner will 'remember' a previously correct double if you don't reset it :
EDITED: Thanks to @Stultuske comment
while (!valid) {
System.out.print("Enter two doubles, seperate by space ");
try {
first = Double.parseDouble(scan.next());
second = Double.parseDouble(scan.next());
valid = true;
}
catch (NumberFormatException e) {
System.out.println("Try again");
scan.nextLine(); // <------- Important line
}
}
In addition to the previous comments, you have to be careful, because the scanner will 'remember' a previously correct double if you don't reset it :
EDITED: Thanks to @Stultuske comment
while (!valid) {
System.out.print("Enter two doubles, seperate by space ");
try {
first = Double.parseDouble(scan.next());
second = Double.parseDouble(scan.next());
valid = true;
}
catch (NumberFormatException e) {
System.out.println("Try again");
scan.nextLine(); // <------- Important line
}
}
edited Nov 14 '18 at 14:35
answered Nov 14 '18 at 13:56
Romain WarnanRomain Warnan
57337
57337
seriously? you really should NOT do that. you would actually create a new instance for each iteration? There are better ways to deal with such issues
– Stultuske
Nov 14 '18 at 14:08
@Stultuske : It will create a new instance each time the user enters a incorrect input, not at each iteration. If you know a better way to reset the content of a scanner, I will be glad to learn it.
– Romain Warnan
Nov 14 '18 at 14:21
it will iterate as long as the user inputs invalid data, so yes, it will create a new instance each iteration, which you really shouldn't. just replace that new instantiation with scan.nextLine(); that's all you need to do
– Stultuske
Nov 14 '18 at 14:28
You are right, thank you for explaining. I will edit what I wrote.
– Romain Warnan
Nov 14 '18 at 14:32
@DogEata Hi, thanks for your suggestion, but I didn't find the scanner will remember the previous correct double. e.g. I typed the 3.0 sss in the first time, and 4.0 5.0 in the second time, the scanner will only show 4.0 and 5.0. Somehow, the scanner didn't remember the input even without the line.
– Z. Zhang
Nov 15 '18 at 1:18
|
show 1 more comment
seriously? you really should NOT do that. you would actually create a new instance for each iteration? There are better ways to deal with such issues
– Stultuske
Nov 14 '18 at 14:08
@Stultuske : It will create a new instance each time the user enters a incorrect input, not at each iteration. If you know a better way to reset the content of a scanner, I will be glad to learn it.
– Romain Warnan
Nov 14 '18 at 14:21
it will iterate as long as the user inputs invalid data, so yes, it will create a new instance each iteration, which you really shouldn't. just replace that new instantiation with scan.nextLine(); that's all you need to do
– Stultuske
Nov 14 '18 at 14:28
You are right, thank you for explaining. I will edit what I wrote.
– Romain Warnan
Nov 14 '18 at 14:32
@DogEata Hi, thanks for your suggestion, but I didn't find the scanner will remember the previous correct double. e.g. I typed the 3.0 sss in the first time, and 4.0 5.0 in the second time, the scanner will only show 4.0 and 5.0. Somehow, the scanner didn't remember the input even without the line.
– Z. Zhang
Nov 15 '18 at 1:18
seriously? you really should NOT do that. you would actually create a new instance for each iteration? There are better ways to deal with such issues
– Stultuske
Nov 14 '18 at 14:08
seriously? you really should NOT do that. you would actually create a new instance for each iteration? There are better ways to deal with such issues
– Stultuske
Nov 14 '18 at 14:08
@Stultuske : It will create a new instance each time the user enters a incorrect input, not at each iteration. If you know a better way to reset the content of a scanner, I will be glad to learn it.
– Romain Warnan
Nov 14 '18 at 14:21
@Stultuske : It will create a new instance each time the user enters a incorrect input, not at each iteration. If you know a better way to reset the content of a scanner, I will be glad to learn it.
– Romain Warnan
Nov 14 '18 at 14:21
it will iterate as long as the user inputs invalid data, so yes, it will create a new instance each iteration, which you really shouldn't. just replace that new instantiation with scan.nextLine(); that's all you need to do
– Stultuske
Nov 14 '18 at 14:28
it will iterate as long as the user inputs invalid data, so yes, it will create a new instance each iteration, which you really shouldn't. just replace that new instantiation with scan.nextLine(); that's all you need to do
– Stultuske
Nov 14 '18 at 14:28
You are right, thank you for explaining. I will edit what I wrote.
– Romain Warnan
Nov 14 '18 at 14:32
You are right, thank you for explaining. I will edit what I wrote.
– Romain Warnan
Nov 14 '18 at 14:32
@DogEata Hi, thanks for your suggestion, but I didn't find the scanner will remember the previous correct double. e.g. I typed the 3.0 sss in the first time, and 4.0 5.0 in the second time, the scanner will only show 4.0 and 5.0. Somehow, the scanner didn't remember the input even without the line.
– Z. Zhang
Nov 15 '18 at 1:18
@DogEata Hi, thanks for your suggestion, but I didn't find the scanner will remember the previous correct double. e.g. I typed the 3.0 sss in the first time, and 4.0 5.0 in the second time, the scanner will only show 4.0 and 5.0. Somehow, the scanner didn't remember the input even without the line.
– Z. Zhang
Nov 15 '18 at 1:18
|
show 1 more 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%2f53301653%2fwrite-a-program-that-will-give-the-use-prompt-to-enter-two-doubles-value%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
2
Try setting
valid
totrue
inside thetry
block. As it is, it'll betrue
regardless of whether an exception was thrown.– jsheeran
Nov 14 '18 at 13:45
@jsheeran I have tried your solution and it really helps me out. This drives me crazy all night, thank you very much.
– Z. Zhang
Nov 14 '18 at 13:52
You need to learn how to debug your code. In fact, universities should teach how to debug code before teaching how to write code.
– Max Vollmer
Nov 14 '18 at 13:52
@MaxVollmer Hi, max. Do you know somewhere to learn how to debug my code? I'm not computer science major during the college time. However, I want to get a master degree of the data science. Most programs suggest the applicants has background in Java, that's why I'm start learning write code with the UCSC extension.
– Z. Zhang
Nov 14 '18 at 13:59
Here is a good start: What is a debugger and how can it help me diagnose problems?
– Max Vollmer
Nov 14 '18 at 14:00