Joe, Thanks for the help. To explain; I have a text file each line has name of a jam, canning date. and amount of in jar. For each line I am trying to create an Jam object using the aforementioned for parameters. Its a exercise I found on the net where you required to create a Jam and pantry class with methods to print out the jams , use jam ,
test for empty etc. Ive got the classes working and tested. The issue is creating a new object for each line of the text file. The 1st run of the loop works fine thereafer no joy, thrown my entire
Java arsenal at it ( not that is much ) of while loops. tried counting plus hasnext(). Ive posted my latest attempt below ..... for comic relief. Going to try your solutions now ( otter than the array one not progressed that far yet). Once again thanks.
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File in = new File("jams.txt");
Scanner dataIn = new Scanner (in);
Scanner scan = new Scanner (System.in);
int count = 0;
while ( dataIn.hasNext()) // continue if there are more tokens
{
count++;
String line = dataIn.nextLine(); // read in 1st line of text file
line = line.trim();
int split = line.indexOf(" ");
String typeJar1 = line.substring(0, split); // Extract Jam name
line = line.substring(split);
line = line.trim();
split = line.indexOf(" ");
String dateJar1 = line.substring(0, split); // Extract Jam date
String toInt = line.substring(split);
toInt.trim();
Scanner getInt = new Scanner (toInt);
int amountJar1 = getInt.nextInt(); // Extract amount
if ( count == 1)
{
Jam Jar1 = new Jam (typeJar1,dateJar1,amountJar1);
Jar1.print();
}
if ( count == 2)
{
Jam Jar2 = new Jam (typeJar1,dateJar1,amountJar1);
Jar2.print();
}
if ( count == 3)
{
Jam Jar3 = new Jam (typeJar1,dateJar1,amountJar1);
Jar3.print();
}
}