Hey can anyone help me..
I wrote a program that store the data in a txt file but i keep getting an error on the format method
the code
import java.util.*;
import java.io.*;
import java.lang.*;
public class StudentAplication
{
public static void main (String args [] )
{
TextFile file = new TextFile();
Scanner input = new Scanner (System.in);
int choice;
try
{
System.out.println("Select your choice:");
System.out.println("1.Fill the file with student information ."+ "\n2.Add a new student to the file"+"\n3.Update the information of a given student" + "\n4.Delete a student from the file "+"\n5.Display the information of a given student" +"\n6.Display the information of all student"+"\n7.Exit");
choice = input.nextInt();
while ( choice != 7)
{
switch (choice)
{
case 1 :
addRecords(file);
break;
case 2 :
Add(file);
break;
case 3 :
Update (file);
break;
case 4 :
Delete(file);
break;
case 5 :
DisplayO(file);
break;
case 6 :
DisplayA();
break;
default :
System.out.println("please select your choice from 1 to 7:");
}//end switch
}//end while loop
}//end try
catch (InputMismatchException imRef)
{
System.out.println(imRef.toString());
}//end catch
catch (Exception e)
{
System.out.println(e);
}
}//end main
/////////////////////////////////////////////////////////////////////////////////////
// Methods //
public static void addRecords (TextFile output)
{
Student record = new Student ();
Scanner input = new Scanner (System.in);
File file = new File ("output.txt");
if (! file.exists())
{
output.OpenFile();
System.out.println("Please enter Student's information:");
System.out.println("Enter Student name: ");
System.out.println("Enter Student ID:");
System.out.println("Enter Student home phone number:");
System.out.println("Enter Student mobile phone number:");
System.out.println("Enter Student date of birth:");
while (input.hasNext())
{
try
{
record.setName(input.next());
record.setId(input.nextInt());
record.setHome(input.next());
record.setMobile(input.next());
record.setDate(input.next());
file.format("%s %d %s %s %s %n",record.getName(),record.getId(),record.getHome(),record.getMobile(),record.getDate());
}//end try
catch (FormatterClosedException e)
{
System.out.println("Error writitng to file");
return ;
}
System.out.println("Please enter Student's information:");
System.out.print("Enter Student name: ");
System.out.print("Enter Student ID:");
System.out.print("Enter Student home phone number:");
System.out.print("Enter Student mobile phone number:");
System.out.print("Enter Student date of birth:");
}//end while
}//end if
else
System.out.println("this action will erase the content of the current file, are you sure you want to erase the current file Y/N?");
if (input.next().charAt(0)=='y' || input.next().charAt(0)=='Y')
{
output.OpenFile();
System.out.println("Please enter Student's information:");
System.out.print("Enter Student name: ");
System.out.print("Enter Student ID:");
System.out.print("Enter Student home phone number:");
System.out.print("Enter Student mobile phone number:");
System.out.print("Enter Student date of birth:");
while (input.hasNext())
{
try
{
record.setName(input.next());
record.setId(input.nextInt());
record.setHome(input.next());
record.setMobile(input.next());
record.setDate(input.next());
file.format("%s %d %s %s %s %n",record.getName(),record.getId(),record.getHome(),record.getMobile(),record.getDate());
}//end try
catch (FormatterClosedException e)
{
System.out.println("Error writitng to file");
return ;
}
System.out.println("Please enter Student's information:");
System.out.print("Enter Student name: ");
System.out.print("Enter Student ID:");
System.out.print("Enter Student home phone number:");
System.out.print("Enter Student mobile phone number:");
System.out.print("Enter Student date of birth:");
}//end while
}
output.closeFile();
sort(output);
}//end Fill method
public static void sort (TextFile output )throws Exception
{
Vector <Student> sv = new Vector <Student> ();
convertToVector(sv,output);
//to sort the vector
int index;
int smallestIndex;
int minIndex;
int length = sv.size();
Student temp;
for (index = 0; index < length-1; index++)
{
smallestIndex = index;
for(minIndex = index + 1; minIndex < length; minIndex++)
if (sv.elementAt(minIndex).getId() < sv.elementAt(smallestIndex).getId())
smallestIndex = minIndex;
temp = sv.elementAt(smallestIndex);
sv.setElementAt( sv.elementAt(index),smallestIndex);//to swap the object
sv.setElementAt(temp,index);
}//end of for loop
convertToFile(sv , output);
}//end sort method
public static void convertToVector (Vector <Student> sv , TextFile output)throws Exception
{
Scanner input = new Scanner(new File("Student.txt"));
Student record = new Student ();
//to write the file into the vector
while (input.hasNext())
{
record.setName(input.next());
record.setId(input.nextInt());
record.setHome(input.next());
record.setMobile(input.next());
record.setDate(input.next());
sv.addElement(record);
}//end while loop
output.closeFile();
}
public static void convertToFile (Vector <Student> sv , TextFile output )throws Exception
{
//to write the vecto into the file
output.OpenFile();
for (int i =0 ; i < sv.size() ; i++ )
file.format("%s %d %s %s %s %n",sv.elementAt(i).getName(),sv.elementAt(i).getId(),sv.elementAt(i).getHome(),sv.elementAt(i).getMobile(),sv.elementAt(i).getDate());
output.closeFile();
} // end method convertToVector
public static void Add (TextFile output)throws Exception
{
Vector <Student> sv = new Vector <Student> ();
Student record = new Student ();
Scanner input = new Scanner (System.in);
int Id;
convertToVector(sv , output);
System.out.print("Enter Student name:");
record.setName(input.next());
System.out.print("Enter student ID:");
Id = input.nextInt();
record.setId(Id);
System.out.print("Enter student home phone number:");
record.setHome(input.next());
System.out.print("Enter student mobile phone number:");
record.setMobile(input.next());
System.out.print("Enter student birth date:");
record.setDate(input.next());
for(int i =0 ; i< sv.size() ; i++)
if ( sv.elementAt(i).getId() > Id)
break;
else
sv.insertElementAt(record , i);
convertToFile(sv , output);
}
public static void Update (TextFile output)throws Exception
{
int Id ;
int i ;
boolean found = false ;
Scanner console = new Scanner (System.in);
Vector <Student> sv = new Vector <Student>();
convertToVector(sv , output);
System.out.print("Enter Student ID :");
Id = console.nextInt();
for ( i=0 ; i < sv.size() ; i++)
{
if(sv.elementAt(i).getId() == Id)
found = true ;
break;
}
if (found)
{
System.out.println("1- Update the name.\n2- Update the home phone number.\n3- Update the mobile phone number.\n4- Exit.");
switch(console.nextInt())
{
case 1 :
System.out.println("Enter the new name:");
sv.elementAt(i).setName(console.next());
break;
case 2 :
System.out.println("Enter the new home phone number:");
sv.elementAt(i).setHome(console.next());
break;
case 3 :
System.out.println("Enter the new mobile phone number:");
sv.elementAt(i).setMobile(console.next());
break;
}
}
else
System.out.print("incorrect ID");
convertToFile(sv , output);
}
public static void Delete ( TextFile output)throws Exception
{
int Id ;
int i ;
boolean found = false ;
Scanner console = new Scanner (System.in);
Vector <Student> sv = new Vector <Student>();
convertToVector(sv , output);
System.out.print("Enter Student ID :");
Id = console.nextInt();
for ( i=0 ; i < sv.size() ; i++)
{
if(sv.elementAt(i).getId() == Id)
found = true ;
break;
}
if (found)
sv.removeElementAt(i);
else
System.out.print("incorrect ID");
convertToFile(sv , output);
}
public static void DisplayO (TextFile output)throws Exception
{
int Id ;
int i ;
boolean found = false ;
Scanner console = new Scanner (System.in);
Vector<Student> sv = new Vector<Student> ();
convertToVector(sv , output);
System.out.print("Enter Student ID :");
Id = console.nextInt();
for ( i=0 ; i < sv.size() ; i++)
{
if(sv.elementAt(i).getId() == Id)
found = true ;
break;
}
if (found)
System.out.println(sv.elementAt(i).toString());
else
System.out.print("incorrect ID");
convertToFile(sv , output);
}
public static void DisplayA ()
{
Scanner input = new Scanner (new File ("Student.txt"));
Student record = new Student ();
System.out.printf("%-12s %-12s %-12s %-12s %-12s ","Name","ID","Home NO.","mobile NO.","Birth Date");
try
{
while (input.hasNext())
{
record.setName(input.next());
record.setId(input.nextInt());
record.setHome(input.next());
record.setMobile(input.next());
record.setDate(input.next());
System.out.printf("%-12s %-12s %-12s %-12s %-12s %n ",record.getName(),record.getId(),record.getHome(),record.getMobile(),record.getDate());
}
}
catch ( NoSuchElementException elelment)
{
System.err.println(elelment);
input.close();
}
}
}
the error i get when compiling
----jGRASP exec: javac -g C:\Documents and Settings\user\My Documents\School ^^\New Folder (2)\StudentAplication.java
StudentAplication.java:110: cannot find symbol
symbol : method format(java.lang.String,java.lang.String,int,java.lang.String,java.lang.String,java.lang.String)
location: class java.io.File
file.format("%s %d %s %s %s %n",record.getName(),record.getId(),record.getHome(),record.getMobile(),record.getDate());
^
StudentAplication.java:148: cannot find symbol
symbol : method format(java.lang.String,java.lang.String,int,java.lang.String,java.lang.String,java.lang.String)
location: class java.io.File
file.format("%s %d %s %s %s %n",record.getName(),record.getId(),record.getHome(),record.getMobile(),record.getDate());
^
StudentAplication.java:229: cannot find symbol
symbol : variable file
location: class StudentAplication
file.format("%s %d %s %s %s %n",sv.elementAt(i).getName(),sv.elementAt(i).getId(),sv.elementAt(i).getHome(),sv.elementAt(i).getMobile(),sv.elementAt(i).getDate());
^
3 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
code}
Edited by: Heila on Apr 25, 2008 2:55 PM