Hi,
Could someone help me with one exercise called "Collection of books".
Programm must provide following functions:
User from Console enters Book. It must be possible to enter Book Name, Book Author and Book Year. This info must be stored in some file.
There must be also possible to look at entered book list in following format from that file:
1991 Java for beginers by John Gelwin
1993 Java for professionals by Kelvin Lee
I wrote code but there is still many problems for me I can`t solve. Maybe you could try to compile my code (done in Eclipse) and correct it step by step. Good ideas are welcome!
Code is below. Thanks!
import java.io.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
public class Gramata extends Applet implements KeyListener {
public void keyPressed(KeyEvent e) {int key = e.getKeyCode();}
public static void main (String[] args) {
File file = new File("myfile.txt");
System.out.println("[The end of input: Ctrl-z]");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Do you want to Enter a book(yes|no)?");
try {
if("yes".equalsIgnoreCase(reader.readLine())){
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
PrintWriter out
= new PrintWriter(new FileWriter("myfile.txt"));
String s;
System.out.print("Enter name of the book: ");
while ((s = in.readLine()) != null ) {
out.println(s); }
in.close();
out.close();
}
else if("no".equalsIgnoreCase(reader.readLine())){
BufferedReader in = new BufferedReader(new FileReader("myfile.txt"));
String s;
s = in.readLine();
while ( s != null ) {
System.out.println("Read: " + s);
s = in.readLine();
}
in.close();
}
else {
System.out.print("Wrong input!");
}
} catch (FileNotFoundException e1) {
System.err.println("File not found: " + file);
}
catch (IOException e2) {
e2.printStackTrace();
}
}
}