writing a Time24 object to a file

Hello all,

Bellow is a snippit of code from a larger program I have been working on. The program offers the user 4 options. Create a file of strings, time24 objects, or Integers. The user enters data of the appropriate type until a sentinel value is entered. After that, I call insertionSortR which is a generic insertion sort method, then I output it to a file. I use the first line of the file to label the file as containing Strings, Time24 objects, or Integers

The problem I'm having is writing the time24 object to a file correctly. It seems to write to the array correctly, but when it writes to the file, it writes this:

This file contains Time24 Items

assignment2.Time24@2a4983

The Strings and Integers write to the files correctly. Can someone point me in the right direction. Maybe I'm not seeing something in my code?

package program2;

import java.io.*;

import java.util.*;

/**

* @author Danny Sperling CIS 676

*/

publicclass Main{

/**

* The Main class offers the user four options to reate a file consisting

* of Strings, Time24 Objects, or Integers. The user enters data of

* the appropriate type until a sentinel value is reached.

* Calls on insertionSortR and binSearch methods when needed.

* The fourth option is to search the file for a specified item

*

* @param args the command line arguments

* @throws IOException If an input or output exception occurred

*/

publicstatic <Textends Comparable><?super T>>

void main(String[] args)throws IOException{

BufferedReader keyboard =null;

FileOutputStream out;

PrintStream p =null;

FileInputStream fStream =null;

DataInputStream in =null;

BufferedReader inputFile =null;

String hour =null;

String userChoice;

PrintWriter pWriter =null;

FileWriter fWriter =null;

BufferedReader fReader =null;

String fileName =null;

String searchFile =null;

String strData;

Object objData;

String integerData;

int intData = 0;

int searchInt;

Integer intNum;

File file;

int objLen = 0;

int i;

int number;

int hourData;

int mid;

int midValue;

int first = 0;

int last = objLen-1;

String searchItem;

do{

// creates keyboard as a buffered input stream

keyboard =new BufferedReader(new InputStreamReader(System.in));

//prompts user to choose 1,2,3,4, or 5 to make a corresponding choice

System.out.println("Please Enter: ");

System.out.println("1 to Create a File of type String: ");

System.out.println("2 to Create a File of type Time24 Object: ");

System.out.println("3 to Create a File of type Integer: ");

System.out.println("4 to Search a File: ");

System.out.println("5 to exit the program: ");

userChoice = keyboard.readLine();//user enters 1,2,3,or 4

// converts a userChoice into an int value and sets = to number

number = Integer.parseInt(userChoice);{

if (number == 1)//user chose option 1 to create strings

{

System.out.println("Please Enter the File Name to Create with a" +

".txt extension: ");

String[] strArr =new String[200];//Create new String Array

fileName = keyboard.readLine();//user names file

file =new File(fileName);//creates the user named file

strData = keyboard.readLine();

i=0;//array index marker

objLen = 0;//array size counter

//Loop to input string data into strArr with sentinel "end"

while (!strData.equals("end") && i < strArr.length)

{

System.out.println("Enter a name and press Enter. " +

"Type 'end' and press Enter when done: ");

strData = keyboard.readLine();

strArr[i] = strData;//sets array index = user input

i++;//increments i for next input

objLen++;//increments size counter

}

//Create a new file output stream connected to fileName

out =new FileOutputStream(fileName);

//Connect print stream to the output stream

p =new PrintStream(out);

//label first line of file to show contains strings

p.print("This file contains Strings" +'\n');

//call generic recursive insertionSort() to order the array

insertionSortR(strArr, objLen-1);

// Now output to a file with for loop

for (i = 0; i < objLen; i++)

p.println(strArr[i]);

}

elseif (number == 2)//user chose option 2 to create Time24 Array

{

Time24[] time24Arr =new Time24[200];//create new Time24[]

System.out.println("Please Enter the File Name to Create with a" +

".txt extension: ");

fileName = keyboard.readLine();//user names file

file =new File(fileName);//Creates user named File

hour = keyboard.readLine();

i=0;//array index marker

objLen = 0;//array size counter

// Create a new file output stream

// connected to "fileName"

out =new FileOutputStream(fileName);

// Connect print stream to the output stream

p =new PrintStream(out);

//Loop for putting Time24 Items into an array with sentinel "99"

while (!hour.equals("99") && i < time24Arr.length){

System.out.println("Please enter the hour and press Enter." +

"Type 99 and press enter to end: ");

hour = keyboard.readLine();

//converts String hour to integer

hourData = Integer.parseInt(hour);

System.out.println("Please enter the minutes and press Enter: ");

String min = keyboard.readLine();

//converts String min to integer

int minData = Integer.parseInt(min);

System.out.println("please enter the seconds and press Enter: ");

String sec = keyboard.readLine();

//converts String sec to integer

int secData = Integer.parseInt(sec);

//combine hourData, minData, & secData to make new Time24 Item

Time24 timeData =new Time24(hourData, minData, secData);

time24Arr[i] = timeData;//Place timeData into index in Array

i++;//increment index

objLen++;//increment array size counter

//loop for putting the strings into the array

}

// Create a new file output stream connected to "fileName"

out =new FileOutputStream(fileName);

// Connect print stream to the output stream

p =new PrintStream(out);

//label first line of file to show contains Time24 Items

p.print("This file contains Time24 Items" +'\n');

//call generic recursive insertionSort() to order the array

insertionSortR(time24Arr, objLen-1);

// Now output to a file with for loop

for (i = 0; i < objLen; i++)

p.println(time24Arr[i]);

}

elseif (number == 3)//user chose option 2 to create Time24 Array

{

System.out.println("Please Enter the File Name to Create with a" +

".txt extension: ");

Integer[] intArr =new Integer[200];//Create new Integer[]

fileName = keyboard.readLine();//user names file

file =new File(fileName);//Create user named file

integerData = keyboard.readLine();

i=0;//Array Index marker

objLen = 0;//Array Size counter

//keep looping till sentinel

while (!integerData.equals("0") && i < intArr.length)

{

System.out.println("Enter a Integer (not 0) and press Enter. " +

"Type 0 and press Enter when done: ");

integerData = keyboard.readLine();

intData = Integer.parseInt(integerData);

intArr[i] = intData;

i++;

objLen++;

//loop for putting the strings into the array

}

// Create a new file output stream

// connected to "fileName"

out =new FileOutputStream(fileName);

// Connect print stream to the output stream

p =new PrintStream(out);

//Label first line of file to show it contains Integers

p.print("This file contains Integers" +'\n');

//call generic recursive insertionSort() to order the array

insertionSortR(intArr, objLen-1);

// Now output to a file with for loop

for (i = 0; i < objLen; i++)

p.println(intArr[i]);

}

/**

* This is a Generic, Recursive version of the insertion sort.

* It sorts Strings, Time24 objects, and Integers.

*

* @param arrTakes String, Time24, or Integer Arrays

* @param objLenthe current size of the array being passed

*

*/

public static<Textends Comparable><?super T>>

void insertionSortR(T[] arr,int objLen)

{

int i, j, n = objLen;

T temp;

//place element at index i into sublist

//from index 0 to i-1 where 1 <= i < n,

for (i = 1; i < n; i++)

{

//index j scans down list from index i looking for correct

// position to locate temp; assigns it to arr at index j

j = i;

temp = arr[i];

//locate insertion point by scanning downward as long as

//temp < arr[j] and we have not encountered the beginning of array

while (j > 0 && temp.compareTo(arr[j-1]) <0)

{

//shift elements up list to make room for insertion

arr[j] = arr[j-1];

j--;

}

//the location is found; insert temp

arr[j] = temp;

}

}

Can someone please please help me figure out why the Time24 data will not write to the file correctly?

[15310 byte] By [djsperlinga] at [2007-11-26 19:51:00]
# 1

Should the runtime read your mind as to what you expect it to "print" when you "print" an object of that type?

> p.println(time24Arr[ i ]);

You got the default behavior. You told it to "print" an object, and so it implicitly invokes the toString() method of that object. The implementation of that method will determine what String is returned to "print".

warnerjaa at 2007-7-9 22:40:44 > top of Java-index,Java Essentials,Java Programming...