i really need to get a problem fix tonight, so im hoping a late night programmer will see this. i made a database program that uses flat files storage. let me post the code (its messy):
import java.io.*;
import java.util.StringTokenizer;
class DataBase
{
private String [] name;
private String [] addr;
private String[] phoneNum;
private static int num;
private static BufferedReader fileIn;
private static PrintWriter fileOut;
public static void main (String[] args) throws IOException
{
String fileName = "file2.txt";
String str;
DataBase list = new DataBase();
BufferedReader cin = new BufferedReader (new InputStreamReader (System.in));
System.out.print("How many contacts? ");
str = cin.readLine();
num = Integer.parseInt(str);
createOutputFile(fileName);
addcontact(num, cin);
/* if (num > 1)
sort();*/
list.parse(fileName);
list.displayList(num);
System.out.print("Do you want to update the list? ");
str = cin.readLine();
/* if (str.equals("yes"))
{
num = 0;
while(l < num)
{
list.update(num);
num++;
}
}*/
System.out.print("Do you want to search a person? ");
str = cin.readLine();
if (str.equals("yes"))
{
System.out.print("Please enter the name of the person.");
str = cin.readLine();
String pNum=list.find(str);
System.out.print("The phone number is " + pNum);
}
}
private static void createOutputFile(String fileName)
{
try
{
fileOut = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
}
catch (IOException e)
{
}
}
private static void openInputFile(String fileName)
{
try
{
fileIn = new BufferedReader(new FileReader(fileName));
}
catch (FileNotFoundException e)
{
}
}
public DataBase()
{
name = new String [10];
addr = new String [10];
phoneNum = new String [10];
}
private void parse(String fileName) throws IOException
{
fileIn = new BufferedReader(new FileReader(fileName));
String str;
while ((str = fileIn.readLine()) != null)
{
int j = 0;
StringTokenizer r = new StringTokenizer (str, ",");
String strname = r.nextToken();
String straddr = r.nextToken();
String strphone = r.nextToken();
name[j] = strname;
addr[j] = straddr;
phoneNum[j] = strphone;
j++;
}
fileIn.close();
}
public static void addcontact (int index, BufferedReader cin) throws IOException
{
String str;
String nameread;
String addrread;
String phoneNumread;
fileOut = new PrintWriter(new BufferedWriter(new FileWriter("file.txt")));
for(int h = 0; h < index; h++)
{
System.out.print("Name: ");
nameread = cin.readLine();
nameread = nameread.trim();
System.out.print("Address: ");
addrread = cin.readLine();
addrread = addrread.trim();
System.out.print("Phone number");
phoneNumread = cin.readLine();
phoneNumread = phoneNumread.trim();
String line = nameread + "," + addrread + "," + phoneNumread;
fileOut.println(line);}
fileOut.close();
}
public void displayList (int n)
{
String s1, s2, s3;
System.out.println("\n Name Address Phone_Number \n");
for (int k = 0; k < n; k ++)
{
s1 = (name[k] + " ").substring(0, 10);
s2 = (addr[k] + " ").substring(0, 10);
System.out.println(s1 + " " + s2 + " " + phoneNum[k]);
}
}
}
it stores name, address, and phone number in a line separated by commas. the parser uses stringtokenizer to separate the elements, and puts them in separate arrays. however, it only seems parse the last line. the info in this line is put into the array positions of 0. it is a problem with the parse method, but i dont see it. anybody know what's wrong?
Question
Deepu Sudhakar
i really need to get a problem fix tonight, so im hoping a late night programmer will see this. i made a database program that uses flat files storage. let me post the code (its messy):
import java.io.*; import java.util.StringTokenizer; class DataBase { private String [] name; private String [] addr; private String[] phoneNum; private static int num; private static BufferedReader fileIn; private static PrintWriter fileOut; public static void main (String[] args) throws IOException { String fileName = "file2.txt"; String str; DataBase list = new DataBase(); BufferedReader cin = new BufferedReader (new InputStreamReader (System.in)); System.out.print("How many contacts? "); str = cin.readLine(); num = Integer.parseInt(str); createOutputFile(fileName); addcontact(num, cin); /* if (num > 1) sort();*/ list.parse(fileName); list.displayList(num); System.out.print("Do you want to update the list? "); str = cin.readLine(); /* if (str.equals("yes")) { num = 0; while(l < num) { list.update(num); num++; } }*/ System.out.print("Do you want to search a person? "); str = cin.readLine(); if (str.equals("yes")) { System.out.print("Please enter the name of the person."); str = cin.readLine(); String pNum=list.find(str); System.out.print("The phone number is " + pNum); } } private static void createOutputFile(String fileName) { try { fileOut = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); } catch (IOException e) { } } private static void openInputFile(String fileName) { try { fileIn = new BufferedReader(new FileReader(fileName)); } catch (FileNotFoundException e) { } } public DataBase() { name = new String [10]; addr = new String [10]; phoneNum = new String [10]; } private void parse(String fileName) throws IOException { fileIn = new BufferedReader(new FileReader(fileName)); String str; while ((str = fileIn.readLine()) != null) { int j = 0; StringTokenizer r = new StringTokenizer (str, ","); String strname = r.nextToken(); String straddr = r.nextToken(); String strphone = r.nextToken(); name[j] = strname; addr[j] = straddr; phoneNum[j] = strphone; j++; } fileIn.close(); } public static void addcontact (int index, BufferedReader cin) throws IOException { String str; String nameread; String addrread; String phoneNumread; fileOut = new PrintWriter(new BufferedWriter(new FileWriter("file.txt"))); for(int h = 0; h < index; h++) { System.out.print("Name: "); nameread = cin.readLine(); nameread = nameread.trim(); System.out.print("Address: "); addrread = cin.readLine(); addrread = addrread.trim(); System.out.print("Phone number"); phoneNumread = cin.readLine(); phoneNumread = phoneNumread.trim(); String line = nameread + "," + addrread + "," + phoneNumread; fileOut.println(line);} fileOut.close(); } public void displayList (int n) { String s1, s2, s3; System.out.println("\n Name Address Phone_Number \n"); for (int k = 0; k < n; k ++) { s1 = (name[k] + " ").substring(0, 10); s2 = (addr[k] + " ").substring(0, 10); System.out.println(s1 + " " + s2 + " " + phoneNum[k]); } } }it stores name, address, and phone number in a line separated by commas. the parser uses stringtokenizer to separate the elements, and puts them in separate arrays. however, it only seems parse the last line. the info in this line is put into the array positions of 0. it is a problem with the parse method, but i dont see it. anybody know what's wrong?
Link to comment
Share on other sites
11 answers to this question
Recommended Posts