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

liran
  • 105
  • 5
  • What are you giving as input? – Yassin Hajaj Jun 12 '16 at 09:35
  • aaa>>>> bbb>>>> 1>>>> ccc>>>> vvv ---->>>> here the error start – liran Jun 12 '16 at 09: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()) {} – neocorp Jun 12 '16 at 09:39
  • i too had the same problem ...Read this-[http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Dhruvam Gupta Jun 12 '16 at 10:03

2 Answers2

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**
Yassin Hajaj
  • 20,020
  • 9
  • 41
  • 81
1

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

Community
  • 1
  • 1
Kaushal28
  • 4,823
  • 4
  • 30
  • 57