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;
}







0















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;
}
}









share|improve this question























  • 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


















0















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;
}
}









share|improve this question























  • 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














0












0








0








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;
}
}









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












3 Answers
3






active

oldest

votes


















1














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");

}

}





share|improve this answer































    0














    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.






    share|improve this answer































      0














      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;
      }
      }





      share|improve this answer
























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        1














        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");

        }

        }





        share|improve this answer




























          1














          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");

          }

          }





          share|improve this answer


























            1












            1








            1







            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");

            }

            }





            share|improve this answer













            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");

            }

            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 '18 at 7:18









            secret super starsecret super star

            1,050316




            1,050316

























                0














                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.






                share|improve this answer




























                  0














                  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.






                  share|improve this answer


























                    0












                    0








                    0







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 17 '18 at 7:17









                    M.GM.G

                    388310




                    388310























                        0














                        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;
                        }
                        }





                        share|improve this answer




























                          0














                          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;
                          }
                          }





                          share|improve this answer


























                            0












                            0








                            0







                            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;
                            }
                            }





                            share|improve this answer













                            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;
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 17 '18 at 7:19









                            CentosCentos

                            200110




                            200110






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                List item for chat from Array inside array React Native

                                Thiostrepton

                                Caerphilly