Status
Not open for further replies.

NedKelly

Wise, Aged Ars Veteran
165
Hi Folks, I'm trying to load a file up as I run my program.
I have a method called onLoad, but I havent been able to get it to load as yet. I have tried a number of things but cant get rid of the error message
C:\jdk1.3.1\bin\Athletics.java:26: ';' expected
onLoad(athId) throws IOException;
This message comes up wether I use the throws or not.
Some code
####Main####
import java.io.*;
import java.awt.*;
import AthletesID;

public class Athletics
{
public static void main(String[] args)
{
//Declare variables
boolean done=false;
//Set array and initialise
AthletesID[] athId = new AthletesID[11];
for (int i = 0; i < 11; i++)
athId=new AthletesID();


onLoad(athId);





fileExist(athId) throws IOException;

while (! done)
{

System.out.println("\t===============================================================");
System.out.println("");
System.out.println("");
System.out.println("\t\tWelcome to the Ned Kelly Athletics Team. ");
System.out.println("");
System.out.println("\t\tPlease enter the events as 100m, 200m, 1000m. ");
System.out.println("");
System.out.println("");
System.out.println("\t===============================================================");
System.out.println("");
System.out.println("");
System.out.println("\tPlease enter a number from the menu choice, and press enter. ");
System.out.println("");
System.out.println("");

int menu=1;
do
{
System.out.println("\t1: New member. ");
System.out.println("\t2: Edit entry. ");
System.out.println("\t3: Delete entry. ");
System.out.println("\t4: Display data. ");
System.out.println("\t5: Display bar chart. ");
System.out.println("\t6: Exit program. ");

menu = DataIn.readLineInt();

switch (menu)
{
case 1: try
{ NewMember(athId);
} catch(IOException e)
{ System.out.println("NewMember IOException "+e);
}
break;
/*case 2: try
{ editEntry(athId);
} catch(IOException e)
{ System.out.println("editEntry IOException "+e);
}
break;
/*case 3: try
{ deleteEntry(athId);
} catch(IOException e)
{ System.out.println("deleteEntry IOException "+e);
}
break;
case 4: try
/* { displayData();
} catch(IOException e)
{ System.out.println("displayData IOException "+e);
}
break;
/*case 5: try
{ barChart(athId);
} catch (IOException e)
{ System.out.println("barChart IOException "+e);
}
break;*/
case 6: try
{ exitProgram(athId);
done = true;
} catch (IOException e)
{ System.out.println("exitProgram IOException "+e);
}
break;
default: System.out.println("Invalid entry, please choose one of the six menu choices. ");

}
}
while (menu < 6);

}

}

####Method####

public static void onLoad(AthletesID[] athId) throws IOException
{
DataInputStream istream;
int id, age;
String name, event;
double time;

istream= new DataInputStream(new FileInputStream("events.dat"));
try
{

for(int i=0;i<11;i++)
{

id = istream.readInt();
name = istream.readUTF();
age = istream.readInt();
event = istream.readUTF();
time = istream.readDouble();

athId.setathNum(id);
athId.setathName(name);
athId.setathAge(age);
athId.setathEvent(event);
athId.setathTime(time);

}
}
catch(IOException e)
{
System.err.println("File not opened");
}

finally
{
istream.close();
}
}
Many thanks for any help.
Regards Ne :confused:
 
C:\jdk1.3.1\bin\Athletics.java:26: ';' expected
onLoad(athId) throws IOException;



  • Line 26 wants a semi-colon
  • onLoad(athId) needs to be in a try-catch block


code:<HR>
Code:
try {
    onLoad(athId);
} catch (IOException ioe) {
    ioe.printStackTrace();
}


and put brackets around that for loop... you might find the Java Code Conventions to be a good read.

edit: and what happens if someone enters 7 or even 6 as a menu choice? Your while (choice < 6) will continue on.


~Kevin
:)
 

NedKelly

Wise, Aged Ars Veteran
165
Hi folks, the code was indented when I pasted it on the message.
Thanks, I missed the brackets around the for loop, and yes the 'while' does keep accepting the &%$(@ number.
I'm getting an end of EOFException at the moment, and the message from the onLoad method which is "File not opened".
###onLoad method###
public static void onLoad(AthletesID[] athId) throws IOException
{
DataInputStream istream;
int id, age, num;
String name, event;
double time;

istream= new DataInputStream(new FileInputStream("a:events.dat"));

System.out.println();
num=arrayExists(athId);
try
{

for(int i=0;i<11;i++)
{

id = istream.readInt();
name = istream.readUTF();
age = istream.readInt();
event = istream.readUTF();
time = istream.readDouble();

athId.setathNum(id);
athId.setathName(name);
athId.setathAge(age);
athId.setathEvent(event);
athId.setathTime(time);

}
}
catch(IOException e)
{
System.err.println("File not opened"+e);
}

finally
{
istream.close();
}
}


###Main####

import java.io.*;
import java.awt.*;
import AthletesID;

public class Athletics
{
public static void main(String[] args)
{
//Declare variables
boolean done=false;
//Set array and initialise
AthletesID[] athId = new AthletesID[11];
for (int i = 0; i < 11; i++)
athId=new AthletesID();
try
{
onLoad(athId);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
try
{
fileExist(athId);
} catch (IOException ioe)
{
ioe.printStackTrace();
}

while (! done)
{

System.out.println("\t===============================================================");
System.out.println("");
System.out.println("");
System.out.println("\t\tWelcome to the Ned Kelly Athletics Team. ");
System.out.println("");
System.out.println("\t\tPlease enter the events as 100m, 200m, 1000m. ");
System.out.println("");
System.out.println("");
System.out.println("\t===============================================================");
System.out.println("");
System.out.println("");
System.out.println("\tPlease enter a number from the menu choice, and press enter. ");
System.out.println("");
System.out.println("");

int menu=0;
do
{
System.out.println("\t1: New member. ");
System.out.println("\t2: Edit entry. ");
System.out.println("\t3: Delete entry. ");
System.out.println("\t4: Display data. ");
System.out.println("\t5: Display bar chart. ");
System.out.println("\t6: Save file. ");
System.out.println("\t7: Exit program. ");

menu = DataIn.readLineInt();

switch (menu)
{
case 1: try
{ NewMember(athId);
} catch(IOException e)
{ System.out.println("NewMember IOException "+e);
}
break;
/*case 2: try
{ editEntry(athId);
} catch(IOException e)
{ System.out.println("editEntry IOException "+e);
}
break;
/*case 3: try
{ deleteEntry(athId);
} catch(IOException e)
{ System.out.println("deleteEntry IOException "+e);
}
break;
case 4: try
/* { displayData();
} catch(IOException e)
{ System.out.println("displayData IOException "+e);
}
break;
/*case 5: try
{ barChart(athId);
} catch (IOException e)
{ System.out.println("barChart IOException "+e);
}
break;*/
case 6: try
{ Save(athId);
} catch (IOException e)
{ System.out.println("Save IOException "+e);
}
break;
case 7: try
{ exitProgram(athId);
done = false;
} catch (IOException e)
{ System.out.println("exitProgram IOException "+e);
}
break;
default: System.out.println("Invalid entry, please choose one of the six menu choices. ");

}
}
while (menu <= 7);

}

}

Any help would be appreciated.
Regards Ne :confused:
 

Ensomnea

Ars Scholae Palatinae
1,109
I am attempting to format his code better for myself and others who are attempting to debug it.

Here goes:

code:<HR>
Code:
  ###onLoad method###
public static void onLoad(AthletesID[] athId) throws IOException {
   DataInputStream istream;
   int id, age, num;
   String name, event;
   double time;<P>   istream= new DataInputStream(new FileInputStream("a:events.dat"));<P>   System.out.println();
   num=arrayExists(athId);<P>   try 
   {
      for(int i=0;i<11;i++)
      {<P>         id = istream.readInt();
         name = istream.readUTF();
         age = istream.readInt();
         event = istream.readUTF();
         time = istream.readDouble();<P>         athId[i].setathNum(id);
         athId[i].setathName(name);
         athId[i].setathAge(age);
         athId[i].setathEvent(event);
         athId[i].setathTime(time);
      }
   } catch(IOException e) {
      System.err.println("File not opened"+e);
   } finally {
      istream.close();
   }
}<P>
###Main####<P>import java.io.*;
import java.awt.*;
import AthletesID;<P>public class Athletics {<P>   public static void main(String[] args) 
   {
   //Declare variables
   boolean done=false;<P>   //Set array and initialise
   AthletesID[] athId = new AthletesID[11];<P>   for (int i = 0; i < 11; i++)
   athId[i]=new AthletesID();<P>   try
   {
      onLoad(athId);
   } catch (IOException ioe) {
      ioe.printStackTrace();
   }<P>   try
   {
      fileExist(athId);
   } catch (IOException ioe) {
      ioe.printStackTrace();
   } <P>   while (! done)
   {<P>      System.out.println("\t===============================================================");
      System.out.println("");
      System.out.println("");
      System.out.println("\t\tWelcome to the Ned Kelly Athletics Team. "); 
      System.out.println("");
      System.out.println("\t\tPlease enter the events as 100m, 200m, 1000m. ");
      System.out.println("");
      System.out.println("");
      System.out.println("\t===============================================================");
      System.out.println("");
      System.out.println("");
      System.out.println("\tPlease enter a number from the menu choice, and press enter. ");
      System.out.println("");
      System.out.println(""); <P>      int menu=0;
      do
      {
         System.out.println("\t1: New member. ");
         System.out.println("\t2: Edit entry. ");
         System.out.println("\t3: Delete entry. ");
         System.out.println("\t4: Display data. ");
         System.out.println("\t5: Display bar chart. ");
         System.out.println("\t6: Save file. ");
         System.out.println("\t7: Exit program. ");<P>         menu = DataIn.readLineInt();<P>         switch (menu)
         {
            case 1: try
            { NewMember(athId);
            } catch(IOException e)
            { System.out.println("NewMember IOException "+e);
            }
            break;
            /*case 2: try
            { editEntry(athId);
            } catch(IOException e)
            { System.out.println("editEntry IOException "+e);
            }
            break;
            /*case 3: try
            { deleteEntry(athId);
            } catch(IOException e)
            { System.out.println("deleteEntry IOException "+e);
            }
            break;
            case 4: try
            /* { displayData();
            } catch(IOException e)
            { System.out.println("displayData IOException "+e);
            }
            break;
            /*case 5: try
            { barChart(athId);
            } catch (IOException e)
            { System.out.println("barChart IOException "+e);
            }
            break;*/
            case 6: try
            { Save(athId);
            } catch (IOException e)
            { System.out.println("Save IOException "+e);
            }
            break;
            case 7: try 
            { exitProgram(athId);
            done = false;
            } catch (IOException e)
            { System.out.println("exitProgram IOException "+e);
            }
            break;
            default: System.out.println("Invalid entry, please choose one of the six menu choices. ");<P>         }
      }<P>      while (menu <= 7);<P>   }<P>}<P>
 
Status
Not open for further replies.