0

I'm trying to put values into an array of type holiday but i get error when i put the values When the index i becomes 1 (in the for loop).

Is this the correct way to receive the values into the constructor?

The error i get:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at ExamQ1.main.main(main.java:13) 

The main:

import java.util.Scanner;

public class main {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    Holiday[] h = new Holiday[4];

    for (int i = 0; i < h.length; i++) {
        System.out.println(i);
        h[i] = new Holiday(in.nextLine(), in.nextLine(), in.nextInt());
    }
}
}

The class:

public class Holiday {
private String name;
private String hebrewMonth;

private int vacationDays;

public Holiday(String name, String hebrewMonth, int vacationDays) {
    this.name = name;
    this.hebrewMonth = hebrewMonth;
    this.setVacationDays(vacationDays);
}

public void setName(String name) {

    this.name = name;
}
public String getName() {
    return this.name;
}
public void sethebrewMonth(String hebrewMonth) {
    this.hebrewMonth = hebrewMonth;
}
public String getHebrewMonth() {
    return this.hebrewMonth;
}

public void setVacationDays(int vacationDays) {
    this.vacationDays = vacationDays;
}

public int getVacationDays() {
    return this.vacationDays;
}

public String toString() {
    String str = "name: " + this.name + ",hebrew Month: "
            + this.hebrewMonth + ",vaction days: " + this.vacationDays;
    return str;
}
}

thank's

4
  • What are you giving as input? Commented Jun 12, 2016 at 9:35
  • aaa>>>> bbb>>>> 1>>>> ccc>>>> vvv ---->>>> here the error start Commented Jun 12, 2016 at 9:38
  • This is not a good way of inserting elements, your scanner might not even return any results so you should wrap things around before doing anything with them like so: while (in.hasNext()) {} Commented Jun 12, 2016 at 9:39
  • i too had the same problem ...Read this-http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods Commented Jun 12, 2016 at 10:03

2 Answers 2

1

Just use in.next() instead of in.nextLine(). Click here to see the difference.

Sign up to request clarification or add additional context in comments.

Comments

1

Don't forget to use next() after having used nextInt() to consume the rest of the unread characters.

for (int i = 0; i < h.length; i++) {
    System.out.println(i);
    h[i] = new Holiday(in.nextLine(), in.nextLine(), in.nextInt());
    in.next();
}

Here is your entry

aaa bbb 1 ccc vvv

If you don't use in.next(), this is what will happen :

LOOP 1 :

in.nextLine()    ->    aaa
in.nextLine()    ->    bbb
in.nextInt()     ->    1

//This looks OK.

LOOP 2 :

in.nextLine()    ->    **UNREAD CHARACTER LEFT AFTER in.nextInt()**
in.nextLine()    ->    ccc
in.nextInt()     ->    vvv **INPUT MISMATCH BECAUSE IT IS NOT AN INT**

8 Comments

but i press Enter after each entry
@liran The problem is that you were not using in.next() after nextInt()
for what the: in.next(); in the for?
@liran to consume the rest of the unread characters after nextInt()
i write in for : if (i < h.length - 1) in.next(); Because he asked me again at the end of the loop type value and I do not want it, this is ok?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.