Hey guys i am having a problem with displaying a single record out of a text file the code is not reaching the "if" function in the while loop of the "getRecord" Function please help.
Everything else is working
import java.io.*;
import java.util.Scanner;
import java.util.InputMismatchException;
public class FileTest
{
//Globals
public static Scanner input = new Scanner(System.in);
public static String name = "";
public static String address ="";
public static int age = 0;
public static int id =0;
//----------------------------------------------------------------------------
public static void main(String[] args) throws IOException {
// TODO code application logic here
String choice = " ";
System.out.println("------Sample File Program------------------------");
System.out.println("- Press [n] Empty file and write ! -");
System.out.println("- Press [a] add a record ! -");
System.out.println("- Press [s] display single record ! -");
System.out.println("- Press [r] display file contents ! -");
System.out.println("- Press [d] delete a record(not implemented) -");
System.out.println("-------------------------------------------------");
System.out.println("\n\n");
choice = input.nextLine();
choice = choice.toLowerCase();
switch(choice.charAt(0))
{
case 'n' :
System.out.println("\n-----Using New file----- ");
getPerson(); //User input
savePerson(); //creates file + Input saved to file
break;
case 'a' :
System.out.println("\n-----Using existing file----- ");
getPerson(); //user input
appendFile(); //input appended to file
break;
case 's':
System.out.println("\n-----Display Single Record------");
GetRecord();//THE PROBLEM
Display();
break;
case 'r':
System.out.println("\n-----Read File Contents------");
readFile();
break;
default : System.out.println("Invalid input");
}
}//END OF MAIN
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//Gets User input
public static void getPerson()
{
System.out.println("Enter Record number");
id = Integer.parseInt(input.nextLine()); //captures new line
System.out.println("Enter name :");
name = input.nextLine();
System.out.println("Enter address :");
address = input.nextLine();
System.out.println("Enter age :");
age = input.nextInt();
}
//----------------------------------------------------------------------------
//This method writtes data to a blank file
public static void savePerson()
{
try {
FileWriter create = new FileWriter("Data.txt"); //creates file
BufferedWriter writefile = new BufferedWriter(create);
writefile.write("\n"+id+" "+name+"\t"+address+ " "+ age);
writefile.close();
create.close();
System.out.println("Data Saved");
}
catch(IOException X)
{
System.out.println("Error File not saved");
}
}
//----------------------------------------------------------------------------
//This method writes data to an exiting file
public static void appendFile()
{
try
{
//file open in append mode
FileWriter append = new FileWriter("Data.txt",true);
BufferedWriter writefile = new BufferedWriter(append);
writefile.write("\n"+id+" "+name+"\t"+address+ " "+ age);
writefile.close();
append.close();
System.out.println("File Written");
}
catch(IOException X)
{
System.out.println("error Append file fail!!");
}
}
//----------------------------------------------------------------------------
public static void readFile()
{
//1.Create BufferReaderas object to read file
// use while loop to read contents of file
try
{
name = "null";
address = "None";
age =0;
id = 0;
BufferedReader filein;
filein = new BufferedReader(new FileReader("Data.txt"));
String line = filein.readLine(); //Stores Line contents
while(line!= null)
{
System.out.println(line); //prints line content to screen
line = filein.readLine();
}
filein.close();
}
catch(IOException X)
{
System.out.println("Error file not read !!");
}
}
//----------------------------------------------------------------------------
//DOES NOT WORK SHOULD SEARCH FILE AND GET RECORD VALUES
public static void GetRecord() throws IOException
{
try
{
Scanner in = new Scanner(System.in);
//blank out fields
name="";
address="";
age=0;
id =0;
System.out.print("Enter Record Number : ");
int pn=Integer.parseInt(in.nextLine());
String nameC;
String addressC;
int ageC =0 ;
int idC;
Scanner fileInp = new Scanner(new File("Data.txt"));
while (fileInp.hasNext())
{
idC= Integer.parseInt(fileInp.nextLine());
nameC = fileInp.nextLine();
addressC=fileInp.nextLine();
age=fileInp.nextInt();
if(pn == idC)
{
id = idC;
name = nameC;
address = addressC;
age = ageC;
break;
}
fileInp.close();
}
}
catch (InputMismatchException e)
{
System.out.println("Error, stock item could not be retrieved.");
e.printStackTrace();
}
}
//----------------------------------------------------------------------------
public static void Display()
{
System.out.println("\n"+id+name+"\t"+address+"\t"+ age);
}
//----------------------------------------------------------------------------
} //END OF CLASS