0

I am feeling quite stupid at this point for not being able to figure out something that is most likely a simple fix. I keep getting the error "Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:662) at java.base/java.lang.Integer.parseInt(Integer.java:770) at searchSorting.main(searchSorting.java:15)" after inputting how many numbers I want to input. Others solutions to this problem just don't seem to apply to me somehow. Thanks for the help

import java.util.Scanner;
import java.util.Arrays;
public class searchSorting 
{
    public static void main (String[]args)
    {
        String line;
        int number, search, item, array[], first, last, middle;
        Scanner in = new Scanner(System.in);

        System.out.print("How many numbers you want to input?: ");
        number = in.nextInt();
        array = new int [number];

        item = Integer.parseInt(in.nextLine());
        double[] values = new double[item];

for (int i = 0; i < values.length; i++) {
    System.out.print("Input number " + i + ": ");
    values[i] = Double.parseDouble(in.nextLine());
}
for (int index = 0; index < 5; index++)
    System.out.print(values[index] + "  ");
    in.nextLine();
    Arrays.sort(values);
    System.out.println("Sorted number is: " + Arrays.toString(values));


System.out.println("Enter the number you are looking for?");
      search = in.nextInt();
      first = 0;
      last = (item - 1);
      middle = (first + last)/2;

      while( first <= last )
      {
         if ( array[middle] < item )
           first = middle + 1;
         else if ( array[middle] == item )
         {
           System.out.println(item + " found at location " + (middle + 1) + ".");
           break;
         }
         else
         {
             last = middle - 1;
         }
         middle = (first + last)/2;
      }
      if ( first > last )
          System.out.println(item + " is not found.\n");
   
}}
kaya ruth
  • 31
  • 2

2 Answers2

0

You call this:

 number = in.nextInt();   

Assuming the user types 123 and ENTER, this call consumes the 123 and leaves the input stream positioned before the end-of-line character.

The next relevant code is

item = Integer.parseInt(in.nextLine());  

The nextLine call advances the input stream past the end-of-line, returning all characters it passed on the way. Since the ENTER key was pressed immediately after 123, the returned value is the emoty string. Which is not an integer.

You need to review your strategy of sometimes scanning numbers (nextInt) and sometimes scanning rest-of-linr (nextLine). Mixing the two needs to be done quite carefully. You might be better advised to stick to the numerical methods (nextInt/nextDouble).

For example, replacing this

 item = Integer.parseInt(in.nextLine()); 

by this

 item = in.nextInt();

automatically handles the line-ending.


From discussion in comments:

I am still confused as to why it's having me input the value a second time on the next line

Making assumptions about how you modified the code since your initial question: it's because you've written code that reads the number twice:

    System.out.print("How many numbers you want to input?: ");
    number = in.nextInt();    // **** first input ****
    array = new int [number]; 

    item = in.nextDouble();   // **** second input ****
    double[] values = new double[item];

Each time you call for in.nextSomething() the Scanner is going to read more input. It should likely just be this:

    System.out.print("How many numbers you want to input?: ");  
    number = in.nextInt();  
    array = new int [number];
    double[] values = new double[number];
J.Backus
  • 1,421
  • 1
  • 3
  • 6
  • Thank you for the extra explanation! This solved the error issue, but now 'Input number 0:' is no longer showing up on the next line. – kaya ruth Sep 11 '20 at 01:06
  • This is one of the reasons I don't use Scanner, but rather BufferedReader. – NomadMaker Sep 11 '20 at 03:09
  • Certainlly, if I want a line-oriented UI as many users seem to expect (so: prompt, and then read that input, repeatedly), then I would be reading by lines and parsing the line. From many beginner postings in SO, the concept of scanning by tokens is hard to grasp. It's superficially attractive but has traps for the unwary. – J.Backus Sep 11 '20 at 11:37
  • @kayaruth - what do you mean by 'not showing up on the next line' -- after what? Your code initially prompts for 'how many numbers?', expects to read **2 values** ('number', and 'item'), and then falls into the loop where it prompts for 'Input number N'. So - are you typing 2 numbers after the first prompt? – J.Backus Sep 11 '20 at 11:44
  • This array `array = new int [number];` appears to be unused, so maybe it and the preceding read of `number` should not be there at all? – J.Backus Sep 11 '20 at 11:47
  • I tried getting rid of array = new int [number] but it won't build since it defines the array that's in the search. And yes it has me put two number values instead of just one – kaya ruth Sep 11 '20 at 12:50
  • The search isn't going to work, then, since nothing is ever placed in 'array'. I would guess it should be searching in 'values'. – J.Backus Sep 11 '20 at 13:39
  • I am still confused as to why it's having me input the value a second time on the next line. – kaya ruth Sep 11 '20 at 13:47
  • updated answer (see the end) because the formatting is too complicated for a comment – J.Backus Sep 11 '20 at 23:24
0

For more info check out Scanner and Integer documentation, it's an excellent resource.

Edit: Try removing line 15 and replacing item with number in the next line

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/27137226) – Neil Locketz Sep 11 '20 at 01:33