I'm taking a java course in school and was given this question: "Write a program that stores an array of the five int values 1, 2, 3, 4, and 5, a Date object for current time, and the double value 5.5 in the file named Exercise17_05.dat."
My professor wanted to modify this by saying, "We will change things up a little on this in that you need to let the user enter in their name and enter the five (5) integer values along with the other two items mentioned."
I figured out how to do the original problem thanks to the amazing people on here. I tried to modify the program with what I know but keep getting "Exception in Thread" errors.
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readLong(DataInputStream.java:416)
at java.io.DataInputStream.readDouble(DataInputStream.java:468)
at java.io.ObjectInputStream$BlockDataInputStream.readDouble(ObjectInputStream.java:3208)
at java.io.ObjectInputStream.readDouble(ObjectInputStream.java:1061)
at cosc_hw17.COSC_HW17.main(COSC_HW17.java:75)
Java Result: 1
I know it has something to do with creating an array and the original reading from the file part. Any help would be greatly appreciated.
What I have is attached below. I updated the code and am still having issues. Error is reading that it cannot find symbol for the numbers' "readObject"
import java.io.*; import java.util.Scanner; import java.util.Arrays; import java.util.Date; public class COSC_HW17 { // main method public static void main(String[] args) throws ClassNotFoundException, IOException { // create scanner Scanner input = new Scanner (System.in); // stores integers in array double [] numbers = new double [5]; // stores the returned array from method double[] ret; //Create an output stream for file ObjectOutputStream output = new ObjectOutputStream( new FileOutputStream("Exercise17_05.dat", true)); // Ask user for name System.out.println("Please Enter Name:"); String name = input.next(); // Ask user for numbers System.out.println("Input Five Integer Values:"); for (int i = 0; i < 5; i++) { numbers[i] = input.nextInt(); } //Write to file // 1. Write name output.writeUTF(name); // 2. Write int array object output.writeObject(numbers); // 3. Write double output.writeDouble(5.5); // 4. Write date object output.writeObject(new java.util.Date()); // 5. Write utf string output.writeUTF("Exercise17_05.dat"); // Close the stream output.close(); //Create an input stream for file ObjectInputStream report = new ObjectInputStream (new FileInputStream("Exercise17_05.dat")); // Read from file // 1. Read name System.out.println("Name: " + name); // 2. Read int array object int[] newNumbers = (int[]) (input.readObject()); System.out.println("Integers: " + Arrays.toString(newNumbers)); // 3. Read double double doubleValue = report.readDouble(); System.out.println("Double value: " + doubleValue); // 4. Read date object Date date = (java.util.Date) (report.readObject()); System.out.println("DateTime: " + date); // 5. Read utf string String fileName = report.readUTF(); System.out.println("File name: " + fileName); // Close the stream input.close(); }
You have to read exactly what you wrote. When you don't, the types don't match up. So for example originally when you did ReadDouble, it encountered the persons name, which led to the error with which you started the question. The most recent piece was more subtle, because an array of doubles was written, but you were attempting to read (cast) it as an array of integers.
So we just follow what happened in the writing:
// Read from file
// 1. Read name
String newName = report.readUTF();
System.out.println("Name: " + newName);
// 2. Read double array object
ret = (double[])report.readObject();
System.out.println("Integers: "+ Arrays.toString(ret));
// 3. Read double
double doubleValue = report.readDouble();
System.out.println("Double value: " + doubleValue);
// 4. Read date object
Date date = (java.util.Date) (report.readObject());
System.out.println("DateTime: " + date);
// 5. Read utf string
String fileName = report.readUTF();
System.out.println("File name: " + fileName);
Actually,there is a casual problem with the code.
int[] newNumbers = (int[]) (input.readObject());
readObject()
is a method of class ObjectInputStream
in the java.io.ObjectInputStream.ObjectInputStream
input
according to the code is object of Scanner class from the java.util.Scanner
package.
your statement input.readObject()
is invalid as it seems to me and should raise:
The method readObject() is undefined for the type Scanner
And the Solution to the Question is:-
public class InputFile {
public File data() throws IOException {
Scanner scan= new Scanner(System.in);
String Name="";
int arr[]=new int[5];
LocalDateTime mydate=LocalDateTime.now();
double dou=5.5;
System.out.println("please enter name");
Name=scan.next();
File file = new File("Exercise17_05.dat");
if(file.createNewFile()){
System.out.println("file.txt File Created in Project root directory");
}else
System.out.println("File file.txt already exists in the project root directory");
FileOutputStream fos=new FileOutputStream("Exercise17_05.dat");
fos.write(Name.getBytes());
for(int i=0;i<arr.length;i++) {
System.out.println("please enter an integer numeral for the index:" +i);
arr[i]=scan.nextInt();
fos.write(arr[i]);
}
System.out.println("the array is:::: " +Arrays.toStrings(arr));
fos.write(mydate.toString().getBytes());
fos.write(String.valueOf(dou).getBytes());
return file;
}
public static void main(String[] args) throws IOException {
InputFile data=new InputFile();
data.data();
}
}
I'd be glad to hear from you
Thank You.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.