Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Java giving me anomalous results again

  1. #1

    Default 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.


    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);
     
     
     
     
    		}  
     
    	}
    }
    Last edited by Programming_Hobbyist; October 26th, 2012 at 01:53 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default 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.

  3. #3

    Default 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.

  4. #4

    Default 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.

  5. #5

    Default Re: Java giving me anomalous results again

    A simple error. Too many hours of continuous programming.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Java giving me anomalous results again

    Take a break, life is too short.....

  7. #7

    Default Re: Java giving me anomalous results again

    A 6 hour break aughta do it.

Similar Threads

  1. Java giving me all the hell!!
    By Mar5000k in forum Member Introductions
    Replies: 1
    Last Post: October 10th, 2012, 07:33 PM
  2. getScreenSize() method giving java.awt.HeadlessException
    By tejz123 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2012, 06:37 AM
  3. [SOLVED] Giving Java the name of a file to be read via command line?
    By lavloki in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 14th, 2010, 10:07 AM
  4. Help with a beginner's java assignment: Survey results
    By lavloki in forum Java Theory & Questions
    Replies: 17
    Last Post: October 14th, 2010, 09:08 AM
  5. Replies: 0
    Last Post: March 5th, 2010, 12:32 AM