I'm trying to get my program to continue asking for input and answering that input
I have this program here that reads a list of numbers from an input file, then asks the user to enter a number. The program then looks into the file and if the number that the user entered is in the file, it will display "This number is in the file" if the number is not in the file, the program will display, "Number is not in file." The program then has to keep asking the user to enter numbers and based on the number entered, the program needs to write back the appropriate response. The first time the user is asked to enter a number, the program works correctly and prints back the correct response, the problem is that after the program asks again to enter a number, it will print back the same response as the first number entered no matter what number is entered whether it's in the file or not. Are my while
loops in the wrong place? Not sure how to fix this.
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);
System.out.print("Enter a number in the file: ");
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if(i < 0)
while(i < 0)
System.out.print("Number is not in filen");
System.out.print("Enter number in the file: ");
s.nextInt();
else if(i >= 0)
while(i >= 0)
System.out.print("This number is in the filen");
System.out.print("Enter number in the file: ");
s.nextInt();
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
add a comment |
I have this program here that reads a list of numbers from an input file, then asks the user to enter a number. The program then looks into the file and if the number that the user entered is in the file, it will display "This number is in the file" if the number is not in the file, the program will display, "Number is not in file." The program then has to keep asking the user to enter numbers and based on the number entered, the program needs to write back the appropriate response. The first time the user is asked to enter a number, the program works correctly and prints back the correct response, the problem is that after the program asks again to enter a number, it will print back the same response as the first number entered no matter what number is entered whether it's in the file or not. Are my while
loops in the wrong place? Not sure how to fix this.
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);
System.out.print("Enter a number in the file: ");
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if(i < 0)
while(i < 0)
System.out.print("Number is not in filen");
System.out.print("Enter number in the file: ");
s.nextInt();
else if(i >= 0)
while(i >= 0)
System.out.print("This number is in the filen");
System.out.print("Enter number in the file: ");
s.nextInt();
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
You need to assign the value.numb = s.nextInt()
otherwise the binary search is always the same.
– matt
Nov 16 '18 at 7:46
Same with i. eg. replace s.nextInt() with,i = Arrays.binarySearch(numb, s.nextInt());
otherwise i never changes and you just keep doing the same thing.
– matt
Nov 16 '18 at 7:51
add a comment |
I have this program here that reads a list of numbers from an input file, then asks the user to enter a number. The program then looks into the file and if the number that the user entered is in the file, it will display "This number is in the file" if the number is not in the file, the program will display, "Number is not in file." The program then has to keep asking the user to enter numbers and based on the number entered, the program needs to write back the appropriate response. The first time the user is asked to enter a number, the program works correctly and prints back the correct response, the problem is that after the program asks again to enter a number, it will print back the same response as the first number entered no matter what number is entered whether it's in the file or not. Are my while
loops in the wrong place? Not sure how to fix this.
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);
System.out.print("Enter a number in the file: ");
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if(i < 0)
while(i < 0)
System.out.print("Number is not in filen");
System.out.print("Enter number in the file: ");
s.nextInt();
else if(i >= 0)
while(i >= 0)
System.out.print("This number is in the filen");
System.out.print("Enter number in the file: ");
s.nextInt();
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
I have this program here that reads a list of numbers from an input file, then asks the user to enter a number. The program then looks into the file and if the number that the user entered is in the file, it will display "This number is in the file" if the number is not in the file, the program will display, "Number is not in file." The program then has to keep asking the user to enter numbers and based on the number entered, the program needs to write back the appropriate response. The first time the user is asked to enter a number, the program works correctly and prints back the correct response, the problem is that after the program asks again to enter a number, it will print back the same response as the first number entered no matter what number is entered whether it's in the file or not. Are my while
loops in the wrong place? Not sure how to fix this.
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);
System.out.print("Enter a number in the file: ");
int numb = s.nextInt();
int i = Arrays.binarySearch(numbers, numb);
if(i < 0)
while(i < 0)
System.out.print("Number is not in filen");
System.out.print("Enter number in the file: ");
s.nextInt();
else if(i >= 0)
while(i >= 0)
System.out.print("This number is in the filen");
System.out.print("Enter number in the file: ");
s.nextInt();
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
java arrays
asked Nov 16 '18 at 7:43
Matt L.Matt L.
278
278
You need to assign the value.numb = s.nextInt()
otherwise the binary search is always the same.
– matt
Nov 16 '18 at 7:46
Same with i. eg. replace s.nextInt() with,i = Arrays.binarySearch(numb, s.nextInt());
otherwise i never changes and you just keep doing the same thing.
– matt
Nov 16 '18 at 7:51
add a comment |
You need to assign the value.numb = s.nextInt()
otherwise the binary search is always the same.
– matt
Nov 16 '18 at 7:46
Same with i. eg. replace s.nextInt() with,i = Arrays.binarySearch(numb, s.nextInt());
otherwise i never changes and you just keep doing the same thing.
– matt
Nov 16 '18 at 7:51
You need to assign the value.
numb = s.nextInt()
otherwise the binary search is always the same.– matt
Nov 16 '18 at 7:46
You need to assign the value.
numb = s.nextInt()
otherwise the binary search is always the same.– matt
Nov 16 '18 at 7:46
Same with i. eg. replace s.nextInt() with,
i = Arrays.binarySearch(numb, s.nextInt());
otherwise i never changes and you just keep doing the same thing.– matt
Nov 16 '18 at 7:51
Same with i. eg. replace s.nextInt() with,
i = Arrays.binarySearch(numb, s.nextInt());
otherwise i never changes and you just keep doing the same thing.– matt
Nov 16 '18 at 7:51
add a comment |
3 Answers
3
active
oldest
votes
Try this
public static void main(String args)throws IOException
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
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 if (i >= 0)
System.out.print("This number is in the filen");
add a comment |
The problem is that you don't peform search after second input, so old result is printed out. You must re-assign user input and perform search for every input:
for(;;) // infinite loop
System.out.println("Number is not in file");
num = s.nextInt(); // re-read user input
int i = Arrays.binarySearch(numbers, numb); // search again based on input
if (i < 0)
System.out.println("Number is not in filen");
else
System.out.print("This number is in the filen");
add a comment |
Just remove the first input and do everything in the loop. The main will become:
public static void main(String args) throws IOException
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
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");
The problem is you don't have exit condition - it will ask forever. You might think for something ;)
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%2f53333440%2fim-trying-to-get-my-program-to-continue-asking-for-input-and-answering-that-inp%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
Try this
public static void main(String args)throws IOException
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
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 if (i >= 0)
System.out.print("This number is in the filen");
add a comment |
Try this
public static void main(String args)throws IOException
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
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 if (i >= 0)
System.out.print("This number is in the filen");
add a comment |
Try this
public static void main(String args)throws IOException
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
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 if (i >= 0)
System.out.print("This number is in the filen");
Try this
public static void main(String args)throws IOException
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
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 if (i >= 0)
System.out.print("This number is in the filen");
edited Nov 16 '18 at 9:07
answered Nov 16 '18 at 7:54
flopcoderflopcoder
777513
777513
add a comment |
add a comment |
The problem is that you don't peform search after second input, so old result is printed out. You must re-assign user input and perform search for every input:
for(;;) // infinite loop
System.out.println("Number is not in file");
num = s.nextInt(); // re-read user input
int i = Arrays.binarySearch(numbers, numb); // search again based on input
if (i < 0)
System.out.println("Number is not in filen");
else
System.out.print("This number is in the filen");
add a comment |
The problem is that you don't peform search after second input, so old result is printed out. You must re-assign user input and perform search for every input:
for(;;) // infinite loop
System.out.println("Number is not in file");
num = s.nextInt(); // re-read user input
int i = Arrays.binarySearch(numbers, numb); // search again based on input
if (i < 0)
System.out.println("Number is not in filen");
else
System.out.print("This number is in the filen");
add a comment |
The problem is that you don't peform search after second input, so old result is printed out. You must re-assign user input and perform search for every input:
for(;;) // infinite loop
System.out.println("Number is not in file");
num = s.nextInt(); // re-read user input
int i = Arrays.binarySearch(numbers, numb); // search again based on input
if (i < 0)
System.out.println("Number is not in filen");
else
System.out.print("This number is in the filen");
The problem is that you don't peform search after second input, so old result is printed out. You must re-assign user input and perform search for every input:
for(;;) // infinite loop
System.out.println("Number is not in file");
num = s.nextInt(); // re-read user input
int i = Arrays.binarySearch(numbers, numb); // search again based on input
if (i < 0)
System.out.println("Number is not in filen");
else
System.out.print("This number is in the filen");
answered Nov 16 '18 at 7:52
Alex SalauyouAlex Salauyou
11.3k43662
11.3k43662
add a comment |
add a comment |
Just remove the first input and do everything in the loop. The main will become:
public static void main(String args) throws IOException
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
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");
The problem is you don't have exit condition - it will ask forever. You might think for something ;)
add a comment |
Just remove the first input and do everything in the loop. The main will become:
public static void main(String args) throws IOException
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
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");
The problem is you don't have exit condition - it will ask forever. You might think for something ;)
add a comment |
Just remove the first input and do everything in the loop. The main will become:
public static void main(String args) throws IOException
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
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");
The problem is you don't have exit condition - it will ask forever. You might think for something ;)
Just remove the first input and do everything in the loop. The main will become:
public static void main(String args) throws IOException
Scanner s = new Scanner(System.in);
int numbers = fileToArray();
Arrays.sort(numbers);
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");
The problem is you don't have exit condition - it will ask forever. You might think for something ;)
answered Nov 16 '18 at 7:54
Veselin DavidovVeselin Davidov
5,9551718
5,9551718
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%2f53333440%2fim-trying-to-get-my-program-to-continue-asking-for-input-and-answering-that-inp%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
You need to assign the value.
numb = s.nextInt()
otherwise the binary search is always the same.– matt
Nov 16 '18 at 7:46
Same with i. eg. replace s.nextInt() with,
i = Arrays.binarySearch(numb, s.nextInt());
otherwise i never changes and you just keep doing the same thing.– matt
Nov 16 '18 at 7:51