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!

Ok I was writing this in linux. I thought maybe that was the issue so I switched over to a windows box and copied the code straight from this web page and pasted it into netbeans. Before I ran it I replaced the filePath section with a prompt that asks for the file path instead of supplying it because I honestly don't know how to supply it in windows due to \ being an escape key. I made a new file with the same numbers in it and put it into the project folder. Then I ran it. Not before, but after all of this. The code didn't display a prompt asking for the file path. Instead it displayed the contents of the file, the max value and the gibberish (different but the same style every time) as when I was using Ubuntu. I tried it again after being thoroughly confused and it displayed nothing except Build successful. It does this every other time. Somebody please help me. This is really starting to make even less sense the more I go.

I use Ubuntu at home too, but at the university we use Windows. To use the file path in Windows in a string, double up the backslashes, like this:
String fileName = "C:\\the\path\\to\\file.txt";

You did not include the code that writes to the file. Please add it, it is vital to understanding your issue.

Also, check the lengths of your arrays, don't use fixed values. I'm specifically talking about line 49.

"The array without the max value is [I@38540408 "

That looks like it's printing the address in memory of some object. Without looking at the code, I can guess that you're asking it to do something like

System.out.println ("The array without the max value is "+array);

Since the array class doesn't override the toString method, it inherits from Object, which simply prints the address. If you want to print the contents of an array, you have to iterate the array, something like this:

System.out.println ("The array without the max value is: ");
for (int i: array) println(i);

EDIT: yep. look at your line 57

OK I remember the whole double backslash thing now. Thanks for that reminder. I included the code for writing it to the file but it was commented out. Also it was faulty. So was the way that I was printing the arrayAfter to the console. I was not traversing the array. I was just printing arrayAfter which was probably just the reference address. The other issues must have been with netbeans on my computer because when I opened it up this morning at school I had no problems with errors or phantom filepaths. I fixed the code and everything works as it was supposed to. Here it is:

/*
 * 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 maxvalue;

/**
 *
 * @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.
        System.out.print("Enter the file path: ");
        Scanner keyboard = new Scanner(System.in);//user input for the file path
        String filePath = keyboard.nextLine();
        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[SIZE_TWO];
            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];
            }

           // Writes new array without the max value to the file
             PrintWriter out = new PrintWriter(filePath);
             for (int i = 0; i < arrayAfter.length; i++) {
                out.print (arrayAfter[i]);
            }

             out.close();
            // Close the file.

            arrayFile.close();
        }
    } // end of main method
} // end of Main class

Now I get to do it all over again but with an arrayList! Should be a bit easier I hope.

Hey Jon thanks for confirming my suspicions! I did try the toString method but it gave me an error so I thought I may have been doing something else. Either way I got it fixed. Thanks for the help!

One question - did you ever test that code with an input file of more than ten integers?
Look at your line 37 - is there any way that if condition can ever be true?

I tested it way before I was having issues with the array and it worked. let me try it again and I'll get back to you in a sec

Well it worked. As in it still reads the file even if it is 11 integers. Is that because I set the size to 10? and if so how would I go about fixing that the proper way?

The instructions say "For the purposes of this lab you must use an array of size 10 to store the list. Your program should produce a meaningful error message if the actual list contains more than 10 integers." That is why I just set it to 10 in the first place. I also didn't have that set at the time I was testing the error loop so that is probably why that worked.

Your line 37:

if (arrayBefore.length >= 11)

only asks if the length of the array is greater than 10, but you already know that it's exactly 10.

Your line 43:

while (arrayFile.hasNextInt() && index < arrayBefore.length)

reads up to 10 integers from the file, but nothing bad happens if there's a number 11. This is okay for the user, but it doesn't meet your requirement for a meaningful error message. There's a simple way to fix this, you can figure it out.

I fixed it by initializing arrayBefore before the error message loop like this:

int[] arrayBefore = null;
       // Open the file.
        System.out.print("Enter the file path: ");
        Scanner keyboard = new Scanner(System.in);//user input for the file path
        String filePath = keyboard.nextLine();
        File file = new File(filePath);
        Scanner arrayFile = new Scanner(file);

         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++;
        
        
        // Error message

And referencing a null object worked for you?

Nope. Looking into how to do it now. This would be much easier if I could just use arrayList. Ill keep trying and post when I figure it out

Here's a hint: don't do it with the array. The code I looked at above can handle all of the requirements. All you need to do is alert the user if they give you >10 items to look at. One line after the block that reads the file will do it.

OK I finally got it. Error message works and the rest of it works fine as well. I'm glad you caught that Jon cause I would have lost some points on that one. We are learning the importance of catching errors by the user so it is very important that it works. Here is my finished code (sans methods):

/*
 * 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 maxvalue;

/**
 *
 * @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 = 9;
        int[] arrayBefore = new int[10];
        int[] arrayAfter = new int[SIZE];
        int index = 0;

        // Open the file.
        System.out.print("Enter the file path: ");
        Scanner keyboard = new Scanner(System.in);//user input for the file path
        String filePath = keyboard.nextLine();
        File file = new File(filePath);
        Scanner arrayFile = new Scanner(file);

        // fill the array before finding max value
        while (arrayFile.hasNextInt() && index < 10) {
            arrayBefore[index] = arrayFile.nextInt();
            index++;
        }

        // Error message that applies if file contains more than 10 integers
        if (arrayFile.hasNextInt()) {
            System.out.println("");
            System.out.println("Your file has too many integers.");
            // If the array is the correct size
        } else {

            // Sorting the array
            Arrays.sort(arrayBefore);
            // Finding the max number
            int maxValue = arrayBefore[SIZE];
            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];
            }

            // Writes new array without the max value to the file
            PrintWriter out = new PrintWriter(filePath);
            for (int i = 0; i < arrayAfter.length; i++) {
                out.print(arrayAfter[i]);
            }

            out.close();
            // Close the file.

            arrayFile.close();
        }
    } // end of main method
} // end of Main class

Thanks again for all the help guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.