aspose file tools*
The moose likes I/O and Streams and the fly likes java I/O Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java I/O" Watch "java I/O" New topic
Author

java I/O

sven sederberg
Greenhorn

Joined: Nov 26, 2007
Posts: 3
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();
}


}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42632
    
  65
Here at JavaRanch we don't like to give people ready-made solutions to problems, but rather try to help them do it themselves, so that they can learn something. So, what is the code supposed to, and what does it do right now? What are the multiple problems you're referring to? What are you struggling with in trying to correct them?


Ping & DNS - my free Android networking tools app
sven sederberg
Greenhorn

Joined: Nov 26, 2007
Posts: 3
This is new code I just edited.
My first question is: how can I add more book parameters? For example " enter author:" and "enter release year:".
Now the cycle after "Enter book name: " doesn`t give me possibility to add anything else.
I hope you understand my question.

import java.io.*;

public class Gramata {
public static void main (String[] args) {

File file = new File("myfile.txt");

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Do you want to add new 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 book name: ");


while ((s = in.readLine()) != null) {
out.println(s);
}

in.close();
out.close();

}

else if("no".equalsIgnoreCase(reader.readLine())){

System.out.print("Do you want to look at book list (yes|no)?");

if("yes".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 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!");
}

}

else {
System.out.print("Wrong input!");
}

} catch (FileNotFoundException e1) {

System.err.println("File not found: " + file);
}

catch (IOException e2) {

e2.printStackTrace();
}


}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java I/O