How do I enter a char as input to break out of a loop that is looking for an integer?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have this program what reads numbers from an input file, then asks the user to enter a number. The program then reads all numbers in the file and if the number entered is in the file, the program prints back, "Number is in file", if the number entered is not in the file, the program prints back, "Number is not in file." I need to also have it be able to quit the program if the user enters 'q', but I am unsure as to how I can do that seeing is that 'q' is a char and the program is looking for an input of an int. I did create a variable char quit = 'q', but I'm not sure where to use it, if this is even the right way to start.
package classwork7_2;
import java.util.*;
import java.io.*;
public class ClassWork7_2 {
public static void main(String args)throws IOException {
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
char quit = 'q';
while (true) {
System.out.print("Enter a number in the file: ");
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
}
public static int fileToArray() throws IOException{
Scanner s = new Scanner(System.in);
int array = new int[7];
System.out.print("Enter name of file: ");
String filename = s.nextLine();
File f = new File(filename);
Scanner inputFile = new Scanner(f);
int i = 0;
while(inputFile.hasNext()){
array[i] = inputFile.nextInt();
i++;
}
inputFile.close();
return array;
}
}
java arrays loops
add a comment |
I have this program what reads numbers from an input file, then asks the user to enter a number. The program then reads all numbers in the file and if the number entered is in the file, the program prints back, "Number is in file", if the number entered is not in the file, the program prints back, "Number is not in file." I need to also have it be able to quit the program if the user enters 'q', but I am unsure as to how I can do that seeing is that 'q' is a char and the program is looking for an input of an int. I did create a variable char quit = 'q', but I'm not sure where to use it, if this is even the right way to start.
package classwork7_2;
import java.util.*;
import java.io.*;
public class ClassWork7_2 {
public static void main(String args)throws IOException {
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
char quit = 'q';
while (true) {
System.out.print("Enter a number in the file: ");
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
}
public static int fileToArray() throws IOException{
Scanner s = new Scanner(System.in);
int array = new int[7];
System.out.print("Enter name of file: ");
String filename = s.nextLine();
File f = new File(filename);
Scanner inputFile = new Scanner(f);
int i = 0;
while(inputFile.hasNext()){
array[i] = inputFile.nextInt();
i++;
}
inputFile.close();
return array;
}
}
java arrays loops
Don't use nextInt(), instead perhaps use:int numb; String str = s.nextLine(); if (str,charAt(0) == 'q') { break; } if (!str.matches("\d+") { System.out.println("Invalid Entry!"); continue; } numb = Integer.parseInt(str); // the reset of your code
– DevilsHnd
Nov 17 '18 at 7:17
Good approach is to user Scanner.hasNextXXX methods as is described in my answer.
– Centos
Nov 17 '18 at 7:44
add a comment |
I have this program what reads numbers from an input file, then asks the user to enter a number. The program then reads all numbers in the file and if the number entered is in the file, the program prints back, "Number is in file", if the number entered is not in the file, the program prints back, "Number is not in file." I need to also have it be able to quit the program if the user enters 'q', but I am unsure as to how I can do that seeing is that 'q' is a char and the program is looking for an input of an int. I did create a variable char quit = 'q', but I'm not sure where to use it, if this is even the right way to start.
package classwork7_2;
import java.util.*;
import java.io.*;
public class ClassWork7_2 {
public static void main(String args)throws IOException {
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
char quit = 'q';
while (true) {
System.out.print("Enter a number in the file: ");
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
}
public static int fileToArray() throws IOException{
Scanner s = new Scanner(System.in);
int array = new int[7];
System.out.print("Enter name of file: ");
String filename = s.nextLine();
File f = new File(filename);
Scanner inputFile = new Scanner(f);
int i = 0;
while(inputFile.hasNext()){
array[i] = inputFile.nextInt();
i++;
}
inputFile.close();
return array;
}
}
java arrays loops
I have this program what reads numbers from an input file, then asks the user to enter a number. The program then reads all numbers in the file and if the number entered is in the file, the program prints back, "Number is in file", if the number entered is not in the file, the program prints back, "Number is not in file." I need to also have it be able to quit the program if the user enters 'q', but I am unsure as to how I can do that seeing is that 'q' is a char and the program is looking for an input of an int. I did create a variable char quit = 'q', but I'm not sure where to use it, if this is even the right way to start.
package classwork7_2;
import java.util.*;
import java.io.*;
public class ClassWork7_2 {
public static void main(String args)throws IOException {
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
char quit = 'q';
while (true) {
System.out.print("Enter a number in the file: ");
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
}
public static int fileToArray() throws IOException{
Scanner s = new Scanner(System.in);
int array = new int[7];
System.out.print("Enter name of file: ");
String filename = s.nextLine();
File f = new File(filename);
Scanner inputFile = new Scanner(f);
int i = 0;
while(inputFile.hasNext()){
array[i] = inputFile.nextInt();
i++;
}
inputFile.close();
return array;
}
}
java arrays loops
java arrays loops
asked Nov 17 '18 at 7:06
Matt L.Matt L.
278
278
Don't use nextInt(), instead perhaps use:int numb; String str = s.nextLine(); if (str,charAt(0) == 'q') { break; } if (!str.matches("\d+") { System.out.println("Invalid Entry!"); continue; } numb = Integer.parseInt(str); // the reset of your code
– DevilsHnd
Nov 17 '18 at 7:17
Good approach is to user Scanner.hasNextXXX methods as is described in my answer.
– Centos
Nov 17 '18 at 7:44
add a comment |
Don't use nextInt(), instead perhaps use:int numb; String str = s.nextLine(); if (str,charAt(0) == 'q') { break; } if (!str.matches("\d+") { System.out.println("Invalid Entry!"); continue; } numb = Integer.parseInt(str); // the reset of your code
– DevilsHnd
Nov 17 '18 at 7:17
Good approach is to user Scanner.hasNextXXX methods as is described in my answer.
– Centos
Nov 17 '18 at 7:44
Don't use nextInt(), instead perhaps use:
int numb; String str = s.nextLine(); if (str,charAt(0) == 'q') { break; } if (!str.matches("\d+") { System.out.println("Invalid Entry!"); continue; } numb = Integer.parseInt(str); // the reset of your code– DevilsHnd
Nov 17 '18 at 7:17
Don't use nextInt(), instead perhaps use:
int numb; String str = s.nextLine(); if (str,charAt(0) == 'q') { break; } if (!str.matches("\d+") { System.out.println("Invalid Entry!"); continue; } numb = Integer.parseInt(str); // the reset of your code– DevilsHnd
Nov 17 '18 at 7:17
Good approach is to user Scanner.hasNextXXX methods as is described in my answer.
– Centos
Nov 17 '18 at 7:44
Good approach is to user Scanner.hasNextXXX methods as is described in my answer.
– Centos
Nov 17 '18 at 7:44
add a comment |
3 Answers
3
active
oldest
votes
You can change the while loop like below:
Here, instead of reading nextInt, reading string and converting to int if it is not q
while (true) {
System.out.print("Enter a number in the file: ");
String ln = s.nextLine();
if("q".equals(ln)) {
break;//exiting as user entered "q"
}
int numb = Integer.parseInt(ln);
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
add a comment |
Scanner s = new Scanner(System.in);
String userInput = s.next();
int result = Integer.parseInt(userInput);
You can take the original number inputs as strings then parse them into an integer and use that result when using your binary search.
add a comment |
Check that if scanner has int or letter 'q':
while (true) {
System.out.print("Enter a number in the file: ");
if (s.hasNextInt()) {
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
if (s.hasNext("q")) {
return;
}
}
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%2f53349036%2fhow-do-i-enter-a-char-as-input-to-break-out-of-a-loop-that-is-looking-for-an-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can change the while loop like below:
Here, instead of reading nextInt, reading string and converting to int if it is not q
while (true) {
System.out.print("Enter a number in the file: ");
String ln = s.nextLine();
if("q".equals(ln)) {
break;//exiting as user entered "q"
}
int numb = Integer.parseInt(ln);
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
add a comment |
You can change the while loop like below:
Here, instead of reading nextInt, reading string and converting to int if it is not q
while (true) {
System.out.print("Enter a number in the file: ");
String ln = s.nextLine();
if("q".equals(ln)) {
break;//exiting as user entered "q"
}
int numb = Integer.parseInt(ln);
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
add a comment |
You can change the while loop like below:
Here, instead of reading nextInt, reading string and converting to int if it is not q
while (true) {
System.out.print("Enter a number in the file: ");
String ln = s.nextLine();
if("q".equals(ln)) {
break;//exiting as user entered "q"
}
int numb = Integer.parseInt(ln);
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
You can change the while loop like below:
Here, instead of reading nextInt, reading string and converting to int if it is not q
while (true) {
System.out.print("Enter a number in the file: ");
String ln = s.nextLine();
if("q".equals(ln)) {
break;//exiting as user entered "q"
}
int numb = Integer.parseInt(ln);
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
answered Nov 17 '18 at 7:18
secret super starsecret super star
1,050316
1,050316
add a comment |
add a comment |
Scanner s = new Scanner(System.in);
String userInput = s.next();
int result = Integer.parseInt(userInput);
You can take the original number inputs as strings then parse them into an integer and use that result when using your binary search.
add a comment |
Scanner s = new Scanner(System.in);
String userInput = s.next();
int result = Integer.parseInt(userInput);
You can take the original number inputs as strings then parse them into an integer and use that result when using your binary search.
add a comment |
Scanner s = new Scanner(System.in);
String userInput = s.next();
int result = Integer.parseInt(userInput);
You can take the original number inputs as strings then parse them into an integer and use that result when using your binary search.
Scanner s = new Scanner(System.in);
String userInput = s.next();
int result = Integer.parseInt(userInput);
You can take the original number inputs as strings then parse them into an integer and use that result when using your binary search.
answered Nov 17 '18 at 7:17
M.GM.G
388310
388310
add a comment |
add a comment |
Check that if scanner has int or letter 'q':
while (true) {
System.out.print("Enter a number in the file: ");
if (s.hasNextInt()) {
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
if (s.hasNext("q")) {
return;
}
}
add a comment |
Check that if scanner has int or letter 'q':
while (true) {
System.out.print("Enter a number in the file: ");
if (s.hasNextInt()) {
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
if (s.hasNext("q")) {
return;
}
}
add a comment |
Check that if scanner has int or letter 'q':
while (true) {
System.out.print("Enter a number in the file: ");
if (s.hasNextInt()) {
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
if (s.hasNext("q")) {
return;
}
}
Check that if scanner has int or letter 'q':
while (true) {
System.out.print("Enter a number in the file: ");
if (s.hasNextInt()) {
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if (i < 0) {
System.out.print("Number is not in filen");
} else {
System.out.print("Number is in filen");
}
}
if (s.hasNext("q")) {
return;
}
}
answered Nov 17 '18 at 7:19
CentosCentos
200110
200110
add a comment |
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%2f53349036%2fhow-do-i-enter-a-char-as-input-to-break-out-of-a-loop-that-is-looking-for-an-int%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
Don't use nextInt(), instead perhaps use:
int numb; String str = s.nextLine(); if (str,charAt(0) == 'q') { break; } if (!str.matches("\d+") { System.out.println("Invalid Entry!"); continue; } numb = Integer.parseInt(str); // the reset of your code– DevilsHnd
Nov 17 '18 at 7:17
Good approach is to user Scanner.hasNextXXX methods as is described in my answer.
– Centos
Nov 17 '18 at 7:44