Java giving me anomalous results again
Greetings. Now this is a serious error, but it can probably be fixed by doing something simple. I read the contents of the saved file, with all the person and other information. I save the contents to a temporary object array, people_array, one object for each person. I tested out the algorithms for reading information from the file - it's saving it to the array effortlessly. When I call the method save list, all the others fields of the object come up null - and instead, all the data is saved the .name attribute - height, weight, eye-color, and attributes. Thus, that savelist as it is now, prints out all the database information. I could theoretically just work with that - but it would be better if I could find the source of this error.
Code :
import java.io.*;
import java.nio.file.*;
import java.nio.charset.Charset;
import static java.nio.file.StandardCopyOption.*;
import java.io.Console;
class linkedlist {
public linkedlist next;
public linkedlist previous;
public String data;
}
class person {
public int height = 0; //height in inches
public int weight = 0; //weight in pounds
public String eyecolor = null; // eye color
public String weight_status = null; // very skinny, skinny, medium, fat, very fat
public String name = null;
linkedlist attributes = new linkedlist();
public person(int height1, int weight1, String eyecolor1, String weight_status1, String name1, linkedlist previous1, linkedlist next1) {
height = height1;
weight = weight1;
eyecolor = eyecolor1;
weight_status = weight_status1;
name = name1;
attributes.previous = previous1;
attributes.next = next1;
}
public person () {
}
}
public class person_manager {
static File file = new File("C:\\Documents and Settings\\Administrator\\My Documents\\sorted_list.txt");
static File file2 = new File("C:\\Documents and Settings\\Administrator\\My Documents\\the_person_modification_database.txt");
static StringBuffer contents = new StringBuffer();
static BufferedReader reader = null;
static BufferedReader reader1 = null;
static int count = 0;
static int listlength = 0;
static String[] list = null;
static String temp1 = null;
static Path file1 = FileSystems.getDefault().getPath("C:\\Documents and Settings\\Administrator\\My Documents\\", "sorted_list.txt");
static person[] people_array = null;
static int arraysize = 0;
//60
public static void main(String[] args) throws IOException {
try {
reader = new BufferedReader(new FileReader(file2));
reader1 = new BufferedReader(new FileReader(file2));
String text = null;
// repeat until all lines are read
while ((text = reader.readLine()) != null) {
contents.append(text).append(System.getProperty("line.separator"));
count++;
}
list = new String[count];
people_array = new person[count];
arraysize = count;
count = 0;
while ((temp1 = reader1.readLine()) != null) {
people_array[count] = new person();
people_array[count].name = temp1;
//System.out.println(people_array[count].name);
if (temp1.substring(0, 2).compareTo("na") == 0 )
{people_array[count].name=temp1.substring(5, temp1.length());}
try {
if (temp1.substring(0, 2).compareTo("he") == 0 )
{people_array[count].height=Integer.parseInt(temp1.substring(7, temp1.length()));}
} catch (NumberFormatException f) {people_array[count].height=0;}
try {
if (temp1.substring(0, 2).compareTo("we") == 0 && (temp1.substring(6, 7).compareTo("-") != 0) )
{
people_array[count].weight=Integer.parseInt(temp1.substring(7, temp1.length()));}
} catch (NumberFormatException f) {people_array[count].weight=0;}
if (temp1.substring(0, 2).compareTo("ey") == 0)
{people_array[count].eyecolor=temp1.substring(10, temp1.length());}
if (temp1.substring(0, 7).compareTo("weight-") == 0)
{people_array[count].weight_status=temp1.substring(14, temp1.length());}
if (temp1.substring(0, 2).compareTo("at") == 0) {
if (temp1.substring(0, 10).compareTo("attributes") == 0) {
people_array[count].attributes.data = temp1;
} else { people_array[count].attributes.data = temp1.substring(10, temp1.length());}
}
else { }
count++;
}
listlength = count;
} catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
finally { try { if (reader != null) { reader.close();} } catch (IOException e) { e.printStackTrace(); } }
//addData();
savelist();
}
public static void savelist () throws IOException {
PrintStream out = new PrintStream(new FileOutputStream("C:\\Documents and Settings\\Administrator\\My Documents\\testing1.txt"));
for (int i = 0 ; i < listlength ; i++) {
System.out.println(people_array[i].name);
}
}
}
Re: Java giving me anomalous results again
Time to throw in some printlns and see what variables have what values. Follow the value through the program and see where it gets lost.
Re: Java giving me anomalous results again
Already did that, it gets lost at the savefile() method. I reckon it has something to do with objects and arrays. Some sort of java unrefinement. It saves the line of the file to the .name property only, it prints 5 blank lines, then it saves the next line to the .name property. Maybe it is some sort of space-saving feature - as the object array is 6 times the size that it needs to be. Maybe some sort of memory management error.
Re: Java giving me anomalous results again
Oh - figured it out:
while ((temp1 = reader1.readLine()) != null) {
people_array[count] = new person();
people_array[count].name = temp1;
I create a new object for each line - and set its name property to the line. Thus explaining the 6 empty lines. Okay I figured it out myself - but it was posting it here that somehow altered my thought process and allowed me to figure that out. So not a waste, heh. Not a java error either.
Re: Java giving me anomalous results again
A simple error. Too many hours of continuous programming.
Re: Java giving me anomalous results again
Take a break, life is too short.....
Re: Java giving me anomalous results again
A 6 hour break aughta do it.