This is homework. I have the code written, though. The thing works. Mostly. The assignment is to write a program that reads a list of integers from a file, prints the maximum value in the list to the console, removes the maximum value from the list, and stores the (smaller) list back to the file.
I have a file that I just typed in
0 1 2 3 4 5 6 7 8 9
as a test and I saved it as a text file. I reference this with the file path inside of the code. Everything works great: It compiles, finds the file, fills the array, prints the array to the console, and finds the max number. The issue I am having is that when I copy the array to arrayAfter and print that as a check it prints out garbage like this:
"The array without the max value is [I@38540408 "
I commented out my save function because it kept saving the random crap to the file. I am at a loss and this thing is due tomorrow. I have commented everything out and debugged like crazy. Could it be something in my text file somehow? Am I doing something terrible in the code?
Here's the code:
/*
* This program reads a list of integers from a file, prints
* the maximum value in the list to the console, removes the maximum value from the list,
* and stores the (smaller) list back to the file.
*/
package lab1a241;
/**
*
* @author 24x24
*/
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// Declaring variables
final int SIZE = 10;
final int SIZE_TWO = 9;
int[] arrayBefore = new int[SIZE];
int[] arrayAfter = new int[SIZE_TWO];
int index = 0;
// Open the file.
String filePath = "/home/24x24/Dropbox/NetBeansProjects/cs241lab1/array.txt";
File file = new File(filePath);
Scanner arrayFile = new Scanner(file);
// Error message that applies if file contains more than 10 integers
if (arrayBefore.length >= 11) {
System.out.println("Your file has too many integers.");
// If the array is the correct size
} else {
System.out.println("The array from the file is ");
// fill the array before finding max value
while (arrayFile.hasNextInt() && index < arrayBefore.length) {
arrayBefore[index] = arrayFile.nextInt();
System.out.print(arrayBefore[index]);
index++;
}
// Sorting the array
Arrays.sort(arrayBefore);
// Finding the max number
int maxValue = arrayBefore[9];
System.out.println("");
System.out.println("The maximum value is " + maxValue);
// Copying the array minus the max value
for (int i = 0; i < arrayBefore.length - 1; i++) {
arrayAfter[i] = arrayBefore[i];
}
System.out.println("The array without the max value is " + arrayAfter);
// Writes new array without the max value to the file
// PrintWriter out = new PrintWriter(filePath);
// out.print(arrayAfter);
// out.close();
// Close the file.
arrayFile.close();
}
} // end of main method
} // end of Main class
Any thoughts would be much appreciated. Also if my code sucks somewhere let me know. Although that is secondary because it has to work before it gets any improvements.
Thanks again!