-2

I'm trying to solve this question, but the NumberFormatException always happens. Using try-catch doesn't give me what I want because it always results in printing out "invalid input!" Please help me.

The following is the code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class BTECtry2 {

    public static int binarySearch(double[] arr2, int L, int R, double x) {
        if (L <= R) {
            int mid = L + (R - L) / 2;
            if (arr2[mid] == x) {
                return mid;
            }
            if (arr2[mid] > x) {
                return binarySearch(arr2, L, mid - 1, x);
            } else {
                return binarySearch(arr2, mid + 1, R, x);
            }

        }
        return -1;
    }

    public static void insertionSort(double[] arr) {
        int x = arr.length;
        for (int i = 0; i < x; i++) {
            int y = i;
            Double money;
            for (int j = i; j < x; j++) {
                if (arr[j] < arr[y]) {
                    y = j;
                }

            }
            if (y != i) {
                money = arr[y];
                arr[y] = arr[i];
                arr[i] = money;
            }
        }

    }

    public static void main(String[] args) throws IOException {
        int[] USDcoin = new int[15];
        double[] JDcoin = new double[15]; // size is 15 for both arrays because there are 15 values in the txt file 
        String coins = "USD.txt";

        BufferedReader reader = new BufferedReader(new FileReader(coins));

        String y;
        int i = 0;
        while ((y = reader.readLine()) != null) {
            int temp = Integer.parseInt(y);
            USDcoin[i] = temp;
            JDcoin[i] = temp * (0.71);
            i += 1;

        }
        while (true) {
            System.out.println("P. to display the array");
            System.out.println("D. to sort the array");
            System.out.println("S. search for a number in array");
            System.out.println("Q. Quit the program");
            System.out.print("Enter your choice from the list above: ");
            Scanner input = new Scanner(System.in);
            String list;
            list = input.next();

            switch (list) {
                case "P" -> {

                    for (int j = 0; j < JDcoin.length; j++) {
                        System.out.println(JDcoin[j]);

                    }

                }

                case "D" -> {
                    insertionSort(JDcoin);
                    for (int j = 0; j < JDcoin.length; j++) {
                        System.out.println(JDcoin[j]);
                    }
                }

                case "S" -> {
                    Double value;
                    System.out.println(" Enter number : ");
                    value = input.nextDouble();
                    int w = binarySearch(JDcoin, 0, JDcoin.length, value);
                    if (w == -1) {
                        System.out.println(" Coin searched for is not found! ");
                    } else {
                        System.out.println(" Coin searched for is found! ");
                    }

                }
                case "Q" -> {
                    reader.close();
                    System.exit(0);

                }

                default -> {

                    System.out.println(" Invalid input! ");
                }

            }

        }

    }

}

The "USD.txt" file is just a txt file with a random list of number as follows:

200
100
400
350
800
750
250
200
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Please provide more details. Which line emits the exception? This code is quite messy and hard to read. – Jakub Moravec Apr 27 '22 at 11:36
  • @JakubMoravec the title says it all – Stultuske Apr 27 '22 at 11:37
  • @Mothy2nd you are trying to parse an empty String, which is not a numerical value, into a numerical value. I assume you know how to use conditional statements? – Stultuske Apr 27 '22 at 11:38
  • @Stultuske I do know how to use conditional statements, but idk how to handle this exception as I've never ran through it before and tbh, idk what the empty string is? Isn't it supposed to take data from the filereader? – Mothy2nd Apr 27 '22 at 11:40
  • 1
    if ( !y.trim().equals("") ) -> only parse in this case. – Stultuske Apr 27 '22 at 11:42
  • @Stultuske will it still read the file and give me correct output? – Mothy2nd Apr 27 '22 at 11:45
  • 1
    it will only prevent trying to parse an empty String into a numerical value, it doesn't change the rest of the functionality – Stultuske Apr 27 '22 at 11:55
  • Please read [mre] and remove everything not related to your problem (including the all-bold formatting) from your question. – Sören Apr 28 '22 at 10:30

1 Answers1

1

I can see a few problems with your code. First, the part where you read in the file and parse the string around the line:

int temp = Integer.parseInt(y);

You should validate the contents of y before passing it into the parser:

  • is it empty?
  • does it match a number? (see regular expressions for that matter)
  • maybe eliminate leading or trailing whitespaces (e.g. with .trim())

Then the switch-case of the user input has the following flaws: it is missing the 'break;' statements at the end of each case. This leads to unwanted behaviour. Look up how a switch-case block is executed espacially what a 'fallthrough' is. Also a switch-case used with a String input is case sensitive. So in your case, when the user inputs "p" (lowercase) it will give you " Invalid input! ", because it does not match your case "P" (uppercase).

Hope this helps a bit and gets you going.

Eskapone
  • 321
  • 1
  • 7