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