I’m working on creating a class file called “MyClass” – it works fine when I reference the class using the main class file and I can also make instances of the class with no problems by using this constructor method:
public MyClass(String x, int y) {
this.x = x;
this.y = y;
}
I run into the NoSuchElementException error when I put it in a loop and let it read data from a text document:
Scanner in = new Scanner(new FileReader(fileLocation));
//fileLocation is a var for the path
MyClass [] items = new MyClass[5];
for(int counter = 0; counter < 5; counter++) {
while(in.hasNextLine()) {
String x = read.next();
int y = read.nextInt();
items[counter] = new MyClass(x, y); //args require String, int
}
}
in.close();
Here is the text file it is pulling from:
string1 644
string2 777
Thank you for any assistance.
Try this