java I/O

Reply

Join Date: Dec 2007
Posts: 1
Reputation: renars1985 is an unknown quantity at this point 
Solved Threads: 0
renars1985 renars1985 is offline Offline
Newbie Poster

java I/O

 
0
  #1
Dec 16th, 2007
Hi,

I created JAVA prorgamm. It works like this: I can enter some book parameters (author, name, year) and programm will write data in file and also will display data in console. Code is below in bold. Compile code below and you will understand how programm works. However I have to create several .jsp pages (web programm) using this code but I`m not so good in .jsp
First question: How can I create .jsp page where I can enter book which will be written in file and then this book parameters (name, author, year) sends out in another .jsp page?
Can someone help me with this? Thanks!


import java.io.*;
import java.util.*;

public class Gramata3 {
public static void main(String[] args) {
String[][] books = {
// book author year
{ "Head First Java", "Bates and Sierra", "2003" },
{ "Thinking In Java", "Bruce Eckel", "2002" },
{ "Learning Java", "Niemeyer and Knudsen", "2000" },
{ "Developing Java Sortware", "Winder and Roberts", "2000" }
};
File file = new File("gramata3.txt");
try {
PrintWriter out = new PrintWriter(
new FileWriter(file));
for(int j = 0; j < books.length; j++) {
out.println(books[j][2] + " " + books[j][0] + " by " + books[j][1]);
}
out.close();
} catch(IOException e) {
System.out.println("write error: " + e.getMessage());
}

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")) {
System.out.println("sort books by title, author " +
"or date? 't'/'a'/'d'");
choice = reader.readLine().toLowerCase();
int index = choice.equals("t") ? 1 :
choice.equals("a") ? 2 : 0;
showBooks(file, index);
}
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("main I/O error: " + e.getMessage());
}
}

private static void showBooks(File file, int index) {
List<String> list = new ArrayList<String>();
try {
BufferedReader in = new BufferedReader(
new FileReader(file));
String line;
while((line = in.readLine()) != null) {
list.add(line);
}
in.close();
} catch(IOException e) {
System.out.println("showBooks I/O error: " + e.getMessage());
}
sort(list, index);
}

private static void sort(List<String> list, int index) {
// Parse the lines into the title, author and date data.
String[][] data = new String[list.size()][];
for(int j = 0; j < list.size(); j++) {
String book = (String)list.get(j);
data[j] = getBookData(book);
}
// Sort the books.
for(int j = 0; j < data.length; j++) {
String min = data[j][index];
int minIndex = j;
for(int k = j+1; k < data.length; k++) {
int compare = 0;
switch(index) {
case 0: // year
Integer n = Integer.valueOf(min);
compare = Integer.valueOf(data[k][index]).compareTo(n);
break;
case 1: // title
String s = String.valueOf(min);
compare = data[k][index].compareTo(s);
break;
case 2: // author
String name = getComparableName(data[k][index]);
s = getComparableName(min);
compare = name.compareTo(s);
}
if(compare < 0) {
min = data[k][index];
minIndex = k;
}
}
if(minIndex != j) {
// Found lower sort value, swap book elements.
String[] temp = data[j];
data[j] = data[minIndex];
data[minIndex] = temp;
}
}
// Print the sorted array.
for(int j = 0; j < data.length; j++) {
String year = data[j][0];
String title = data[j][1];
String author = data[j][2];
System.out.println(year + " " + title + " by " + author);
}
System.out.println("------------------");
}

private static String[] getBookData(String book) {
String[] data = new String[3];
int start = book.indexOf(" ");
data[0] = book.substring(0, start); // year
int by = book.indexOf(" by ", start+1);
data[1] = book.substring(start, by); // title
data[2] = book.substring(by + 4); // author
return data;
}

private static String getComparableName(String s) {
boolean hasAnd = s.indexOf(" and ") != -1;
int last = s.lastIndexOf(" ");
if(!hasAnd && last != -1) {
// Put last name first for accurate sorting.
s = s.substring(last+1) + " " + s.substring(0, last);
}
return s;
}
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JSP Forum
Thread Tools Search this Thread



Tag cloud for JSP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC