0

When the case is called, the user doesn't get a chance to input either the drug name or the barcode. For some reason, Java skips over that portion and calls the function, searches through it, and returns that nothing is found.

public static void main(String[] args) {
    
    readCSV("C:\\CSVDemo.csv");
    
    Scanner sc=new Scanner(System.in);
    System.out.print("Please enter a number associated with the correct task."
            + "\n1: Search by barcode. \n2: Search by drug name. \n");
    int chooseCase=sc.nextInt();
   
    switch (chooseCase) {
        case 1:
            System.out.println("Please enter the barcode that you wish to search: ");
            String userInput = sc.nextLine();
             
            searchBarcode(userInput);
            break;
        case 2:
            System.out.println("Please enter the drug name that you wish to search: ");
            userInput = sc.nextLine();

            searchDrugList(userInput);
            break;
    }
}
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
roziebear
  • 1
  • 2
  • 1
    https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Roddy of the Frozen Peas Dec 07 '20 at 19:47
  • 1
    Almost certainly it's that answer Roddy linked too, this question comes up all the time. You call `nextInt()` and then call `nextLine()`. You have to call `nextLine()` again, right after the call to `nextInt()` because there's actually a newline where you pressed `Enter` sitting there that didn't get read. That's why it appears your current code doesn't work, it's reading the new line you typed before this one. – markspace Dec 07 '20 at 20:04
  • Thanks. I saw a few things on it but I didn't understand what they meant. – roziebear Dec 09 '20 at 10:41

0 Answers0