+ 1

Why String inputting is not working in my code? Please try to solve for me.

I have tried Character variable instead of String but no use. I cut the whole sentence inputting to the next line of Scanner declaration and it works. But I don't know why. If you know, please tell me. Code Playground Link: https://code.sololearn.com/cn0eiGOs2QRv/#java import java.util.Scanner; public class LinearSearch { public static void main(String[] args){ Scanner scan = new Scanner(System.in); // System.out.print("Enter dimention of array: "); // int input1 = scan.nextInt(); System.out.print("Enter array size:"); int input2 = scan.nextInt(); int[] arr = new int[input2]; for (int i = 0; i<arr.length; i++){ System.out.print("Enter a number to the array: "); arr[i] = scan.nextInt(); } int search; System.out.print("Do you want to find a number? [Y/N]"); String some = scan.nextLine(); if(some.equals("Y") || some.equals("y")){ System.out.print("Enter a number to find in the array: "); search = scan.nextInt(); while(some.equals("Y") || some.equals("y")) { for (int i = 0; i < arr.length; i++) { if (arr[i] == search) { System.out.println("Your number is at " + i); System.out.print("Do you want to find again?"); some = scan.nextLine(); break; } else if (i == arr.length-1) { System.out.println("Sorry. Your number is not in the array."); System.out.print("Do you want to find again?"); some = scan.nextLine(); break; }else { continue; } } } } } }

21st Sep 2018, 3:00 PM
Aiden
Aiden - avatar
4 odpowiedzi
+ 1
@Aiden Here's the fixed code. All you need to know is to put "scan.nextLine()" before you use it again. This fixes a logic error with how to Scanner interprets the nextLine() method. https://code.sololearn.com/cDiO5DVd86nX/#java
24th Sep 2018, 6:16 PM
Denneisk
Denneisk - avatar
+ 2
Very good code, but I see where your problems lie. some = scan.nextLine(); is broken due to a way the Scanner interprets inputs. To fix this, it's very easy. Simply do scan.nextLine(); before it without making it initialize anything. The reason for this is because of how the Scanner interprets inputs. Technically, everything you input is saved into the Scanner. That means if you put in "3 1 2 3" into the first int input2 = scan.nextInt(); will be auto completed because on .nextInt(), Scanner stops at every space. So when it continues scanning, it reads the stuff after the space. Where your String some = scan.nextLine(); breaks is because the .nextLine(); interpreter ends at the End of Line (EOL) character, which is an invisible character used to note an end of a line (which is inputted whenever you press Enter). The scanner only reads the EOL character from your last inputs and doesn't go ahead because it thinks it's at the end. Edit: Also, providing a Code Playground link to this would be very appreciated.
21st Sep 2018, 7:50 PM
Denneisk
Denneisk - avatar
+ 1
Thanks for your answer and sorry for late reply, Denneisk. Here's the codeplayground link. I just uploaded. https://code.sololearn.com/cn0eiGOs2QRv/#java
24th Sep 2018, 9:20 AM
Aiden
Aiden - avatar
0
@Denneisk Can you explain it again for solving the code? Cause removing String some variable would be unable to make a while loop. But I tried making a new object for Scanner class. And replace String some = scan.nextLine(); with String some = scan1.nextLine(); and it works. But still there's a problem for new code. After finding a number in the array, the second time finding comes, it happens just like the old one. So, please, if you edit the code and answer in the comment, I would be very appreciated.
24th Sep 2018, 9:46 AM
Aiden
Aiden - avatar