Question

This error has come up when I try to run this:

public class gift1 {
    public static void main(String[] args) throws IOException{


        //declare and initialize scanner to read from gift1.in
        Scanner scan = new Scanner(new File("gift1.in"));
        //declare and initialize PW to write result
        PrintWriter out = new PrintWriter(new File("gift1.out"));

        int np = scan.nextInt();
        List<String> people = new ArrayList<String>();
        for(int o = 1; o<np; o++)
        {
            people.add(scan.next());
        }
        Map<String, Integer> monReceived = new HashMap<String, Integer>();
        for(String person : people)
        {
            monReceived.put(person, 0);
        }
        Map<String, Integer> Initial = new HashMap<String, Integer>();
        for(int i = 0; i < np; i++)
        {
            String person = scan.next();
            int amount = scan.nextInt();
            int giveto = scan.nextInt();

            Initial.put(person, amount);

            int amountGift = 0;
            if(giveto > 0)
            {
                amountGift = (amount/giveto);
                monReceived.put(person, monReceived.get(person) + (amountGift%giveto));
            }

            for(int j = 0; j < giveto; j++)
            {
                String receivers = scan.next();
                monReceived.put(receivers, monReceived.get(receivers) + (amountGift - amountGift%giveto));

            }
        }
        for(String person : people)
        {
            out.println(person + " " + (monReceived.get(person) - Initial.get(person)));
        }
        out.close();
        System.exit(0);
    }
}

do you know why this is? Do i need to add a try/catch? that is the only thought I have had. I have considered changing and using a BufferedReader but that reduces functionality that I need. What is the problem with my code? Thanks, Sam.

More details:

It says the errors are at:

at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at gift1.main(gift1.java:24)

so I assume it is to do with the scanner but I can't see a problem hence I thought I should try try/catch but it hasn't worked as Ive done it.`

edit:the input data is shown below: the first line is an int so I don't understand the error.

   10 
    mitnik 
    Poulsen 
    Tanner 
    Stallman 
    Ritchie 
    Baran 
    Spafford 
    Farmer 
    Venema 
    Linus 
    mitnik 
    300 3 
    Poulsen 
    Tanner 
    Baran 
    Poulsen 
    1000 1 
    Tanner 
    Spafford 
    2000 9 
    mitnik 
    Poulsen 
    Tanner 
    Stallman 
    Ritchie 
    Baran 
    Farmer 
    Venema 
    Linus 
    Tanner 
Was it helpful?

Solution

You are reading number of persons 10 but you are handling only 9 of them. Problem is your for loop. You are iterating from 1 till 9 (9 is last number <10).

You should start iterating from 0 so change

for(int o = 1; o<np; o++)

to

for(int o = 0; o<np; o++)

Otherwise you will be reading 9 persons so you will handle only

mitnik 
Poulsen 
Tanner 
Stallman 
Ritchie 
Baran 
Spafford 
Farmer 

which will leave

Linus 
mitnik 
300 3 
Poulsen 
Tanner 
Baran 
Poulsen 
1000 1 
Tanner 
.
.
.

so in your next loop

String person = scan.next();
int amount = scan.nextInt();
int giveto = scan.nextInt();

String person = scan.next(); will return Linus, but int amount = scan.nextInt(); will try to parse mitnik which is not integer which will throw InputMismatchException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top