View Single Post
  #1 (permalink)  
Old 12-05-2007, 11:20 PM
renars1985 renars1985 is offline
Member
 
Join Date: Nov 2007
Posts: 9
renars1985 is on a distinguished road
continuation of Java I/O
Thanks to hardwired about this code but I forgot ask one more thing:
it is necessary to order entered books by year, author or name.

import java.io.*;

public class Gramata2 {
public static void main (String[] args) {
File file = new File("gramata2.txt");
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
try {
System.out.println("Enter a book, see book list " +
"or quit? 'e'/'s'/'q'");
String choice = reader.readLine();
while(!choice.equalsIgnoreCase("q")) {
if(choice.equals("e")) {
System.out.print("Enter name of the book: ");
String book = reader.readLine();
System.out.println("enter author");
String author = reader.readLine();
System.out.println("enter year");
String year = reader.readLine();
PrintWriter out = new PrintWriter(
new FileWriter(file, true));
out.println(year + " " + book + " by " + author);
out.close();
} else if(choice.equals("s")) {
BufferedReader in = new BufferedReader(
new FileReader(file));
String line;
while((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
System.out.println("Enter a book see, book list " +
"or quit? 'e'/'s'/'q'");
choice = reader.readLine();
}
reader.close();
} catch(IOException e) {
System.out.println("I/O error: " + e.getMessage());
}
}
}
Reply With Quote
Sponsored Links