Results 1 to 3 of 3

Thread: Java I/o

  1. #1
    renars1985 is offline Member
    Join Date
    Nov 2007
    Posts
    9
    Rep Power
    0

    Question 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();
    }


    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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());
            }
        }
    }

  3. #3
    renars1985 is offline Member
    Join Date
    Nov 2007
    Posts
    9
    Rep Power
    0

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •