Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-02-2007, 11:35 PM
Member
 
Join Date: Nov 2007
Posts: 9
renars1985 is on a distinguished road
Java I/o
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();
}


}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-03-2007, 02:47 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
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()); } } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-04-2007, 09:08 PM
Member
 
Join Date: Nov 2007
Posts: 9
renars1985 is on a distinguished road
Thank you very much!
It works.
However I forgot to add one more feature which is necessary: there must be possibility to order entered books by year, author and name.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 03:41 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org