0

I'm trying to write a program that can detect the largest sum that can be made with any subset of numbers in an ArrayList, and the sum must be lower than a user-input target number. My program is working flawlessly so far, with the exception of one line (no pun intended). Keep in mind that this code isn't complete yet too.

For some reason, I keep getting an unusual InputMismatchException on the line I marked below, when invoking my Scanner. When I run the program and ask it to apply the default list (as you will see in the code), I get an unusual output

Enter integers (one at a time) to use, then type "done" 
OR
You can just type "done" to use the default set
done   //User Input here
Done
Enter your target number

After this line, the exception is thrown and I'm kicked out of the program (being written and run in BlueJ). You guys think you could help me out? I'm assuming it's just a "grammatical" error, if you know what I mean. Code Below:

import java.util.*;
/** This program will provide the largest sum of numbers within a set that is less than a user-implied target number. This is done by asking for a user input of integers (or none, for a default set), sorting the list, and calulating the highest sum starting with the largest number and moving downward on the set
   @param input A Scanner used to detect user input
   @param user A placeholder for user-implied integers
   @param target The user-implied target number
   @param index A reference to the currently "targeted" index on "list"
   @param splitIndex Used if the list needs to be checked in non-adjacent indexes
   @param finalOutput The final product of this program
   @param list An ArrayList of Integers used as the set*/
class LargestSum{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int user = 0, target = 0, index = 0, finalOutput = 0, splitIndex = 0;
        ArrayList<Integer> list = new ArrayList<Integer>();
        System.out.println("Enter integers (one at a time) to use, then type \"done\" \nOR\n You can just type \"done\" to use the default set");
        /** A try-catch block is used to detect when the user has finished ading integers to the set*/
        try{
            user = input.nextInt();
            list.add(user);
        }
        catch(InputMismatchException e){
            System.out.print("Done");
            if (list.size() == 0){
                list.add(1);
                list.add(2);
                list.add(4);
                list.add(5);
                list.add(8);
                list.add(12);
                list.add(15);
                list.add(21);
            }
            if (list.size() > 0){
            Collections.sort(list);
            System.out.println("\nEnter your target number");
            target = input.nextInt();           //EXCEPTION IS THROWN HERE
            index = list.size() - 1;
            while (list.get(index) > target){                
                list.remove(index);
                index = index - 1;
                if (index == -1){
                    System.out.println("No sum can be made that is lower than the target number");
                    System.exit(0);
                }
            }        
            while (finalOutput < target){
                if(list.get(index) + list.get(index - 1) < target){
                   finalOutput = list.get(index) + list.get(index - 1);
                   list.remove(index);
                   list.remove(index - 1);
                   list.add(finalOutput);
                   index = list.size() - 1;
                }
                else{
                    for (int i = index; i >= 0; i--){
                        if (list.get(i) + list.get(index) < target){
                            finalOutput = list.get(i) + list.get(index);
                            list.remove(i);
                            list.remove(index);
                            list.add(finalOutput);
                            index = list.size() - 1;
                            i = index;
                        }
                    }
                }
            } 
        }
            System.out.println("Done!");
            System.out.println("Largest Sum is: " + finalOutput);
        }
    }           
}

Thanks guys, and you can ignore the documentation comments. As I said, the program isn't quite complete yet and I plan on adding more.

~AndrewM

Andrew M
  • 93
  • 8
  • 1
    Once you get InputMismatchException you need to consume that `done` phrase. See this [answer](http://stackoverflow.com/questions/3572160/how-to-handle-invalid-input-using-scanner-and-try-catch-currently-have-an-infin) – Bunti Mar 17 '16 at 00:56
  • That did it! Thanks mate, if you make an answer I'll happily give you the check. – Andrew M Mar 17 '16 at 01:05
  • Glad that it helped you. I m not crazily racing for scores. My goal is to help people in every possible way. – Bunti Mar 17 '16 at 01:45

2 Answers2

0

you need to call input.nextLine(); in order to allow the scanner to take in another line of input. Right now input.nextInt() is trying to convert Done to an int causing the InputMismatchException because its using the same input line you typed in previously.

input.nextLine();
System.out.println("\nEnter your target number");
target = input.nextInt();           //EXCEPTION IS THROWN HERE
Raymond Holguin
  • 818
  • 2
  • 9
  • 30
0

The issue is here - user = input.nextInt(); Until you are capturing integers through nextInt() your code is going fine. But you get inputMistmathexception when you provide "done" to this function which is obviously InputMismatchException.Hope that helps.

vv88
  • 153
  • 2
  • 14