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-13-2007, 04:36 PM
Member
 
Join Date: Nov 2007
Posts: 9
renars1985 is on a distinguished road
java IO
Hi,
this is also continuation of my previous question.
following code in Bold is working fine but it resets my entered info each time I do Debug or Run. I what my data not to be overwritten each time.





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;
}
}










dmacvittie suggested to comment following lines but then programm is not working:

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




Can someone help me please. Thanks!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-31-2007, 06:55 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by renars1985 View Post
Hi,
...
it resets my entered info each time I do Debug or Run. I what my data not to be overwritten each time.
If I understand your problem correctly, you need to save your user-entered data in your program so that every time you run your program the specific data entered can be retrieved, correct? First, of course it's going to reset your user-entered info every time you run your program. This is because your program is running in memory(RAM) which is temporary. If you want to have the ability to not have to rewrite your information every time you run your application then you need a method for writing to and retrieving the already saved data from your hard disk.
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:25 AM.


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