Question

What's up y'all,

I am trying to write some code in Java that will read in the numbers from a file (one # on each line of .txt file) put them into an array, and then run quick sort on the array. Eclipse is showing some red that I am having trouble with. My errors are marked with comments, and what the error is, if anyone can help me get this to run, thanks everyone!

-Kyle

OK, I updated with the first two answers, Thanks so far, but two more errors Im not really understanding.

import java.io.*;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;


public class Lab3 {

public static void main(String[] args) throws IOException{


    System.out.print("Name of file with array: ");
    Scanner readIn = new Scanner(System.in);
    String input=readIn.nextLine();}
**testScan1(input);** //Return Type for method is missing (but I am trying to call the method here)


public static void testScan1(String filename)

{
    File file = new File(filename);
    Scanner scan;
    int [] array = new int[5];
    try{


        scan = new Scanner( file );
    }
    catch ( java.io.FileNotFoundException e )
    {
        System.out.println( "couldn't open. file not found "  );
        return;
    }
    while(scan.hasNext())
    {
        for( int i = 0; i <= file.length(); ++i)
        {

            **array[i]=scan.next();** /*Type mismatch, cannot convert from sting to int. (I moved the declaration about try?)*/




        }

        int partition(int arr[], int left, int right)
        {
            int i=left; int j = right;
            int tmp;
            int pivot = arr[(left+right)/2];
            while (i<=j){
                while(arr[i]<pivot)
                    i++;
                while (arr[j]>pivot)
                    j--;
                if (i<=j){
                    tmp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=tmp;
                    i++; j--;
                }
            }
            return i;
        }
        void quickSort(int arr[], int left, int right){
            int index = partition(arr, left, right);
            if (left<index-1);
            quickSort(arr, left, index-1);
            if (index<right)
                quickSort(arr, index, right);
        }
    }
Was it helpful?

Solution

Some errors:

  • public testScan1(String filename) actually doesn't have any return type and it's also called from a static context but it's not static. It should be changed to public static void testScan1(String filename).
  • what is the purpose of file.hasNext()? Of course it doesn't exist because it doesn't mean anything. I think you meant scan.hasNext().
  • array cannot be found because it is defined inside a try/catch block so it's present only inside that scope. Move the definition before try.

In addition try to indent code in a more readable way, cause it's really difficult to find errors. For example why there is a brack } before the call of testScan that falls out of main method from which I suppose you want to call it?

OTHER TIPS

Anytime you're dealing with a recursive algorithm and you get a stack overflow, it's because your algorithm doesn't have a clearly defined edge case that will cause your recursion to terminate. (Or your input is just too big, but that's rarely the case, and isn't the case here.)

You should look at your quickSort() method to see what might be making it call itself infinitely. Think of looking at a reflection with two mirrors, where the reflection bounces off of the other reflection and it goes off into infinity... that is what is happening here.

Also, in the Java language, it is recommended to always start your class name with a capital letter. I would name your class QuickSortHomework or something like that.

Additionally, you might want to read up on how the if statement works in Java, and how "blocks" are defined. You have an if statement near a semicolon and a pair of curly brackets that is probably not doing what you think it's doing.

Honestly, I'm getting kind of annoyed at all the reposts and reposts here.

This isn't what you want to hear, but I feel you're using this site as a crutch. You don't seem to be putting in the time to puzzle out for yourself what's going on. That puzzling out process, no matter how painful, is where real learning comes from.

In this case, if you looked up what that error means, and then you just looked at your quickSort() implementation I think you would have to notice there's something very obviously wrong with it.

EDIT: if you're thinking "but I did try to puzzle it out"... it really helps to include in your post, "I thought it might be this, but that didn't work, so I thought maybe it could be...." etc. Half the time you'll suddenly realize the problem while you're trying to talk through it like this. The other half, at least we see that you're trying.

As I told you in the previous question array cannot be found because it's still in try block.

Then for printing you cannot directly print an array in an useful way, you should iterate over every element and print it in the following way:

for (int i = 0; i < array.length; ++i)
     System.out.println(array[i]+" ");

Here you are:

import java.io.*;
import java.io.File;
import java.util.Scanner;

public class sdfs
{
    public static void main(String[] args) throws IOException
    {
        System.out.print("Name of file with array: ");
        Scanner readIn = new Scanner(System.in);
        String input = readIn.nextLine();   
    }

    public static void testScan1(String filename)
    {
        File file = new File(filename);
        Scanner scan;
        int[] array;

        try
        {
            array = new int[5];
            scan = new Scanner(file);
        }
        catch (java.io.FileNotFoundException e)
        {
            System.out.println("couldn't open. file not found ");
            return;
        }
        while (scan.hasNext())
        {
                for (int i = 0; i <= file.length(); ++i)
                {
                    array[i] = Integer.parseInt(scan.next()); 

                    for (int j = 0; j < array.length; ++j)     
                        System.out.println(array[i]+" ");
                }
        }
    }

    int partition(int[] arr, int left, int right)
    {
        int i = left;
        int j = right;
        int tmp;
        int pivot = arr[(left + right) / 2];
        while (i <= j) {
                while (arr[i] < pivot)
                        i++;
                while (arr[j] > pivot)
                        j--;
                if (i <= j) {
                        tmp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = tmp;
                        i++;
                        j--;
                }
        }
        return i;
    }

    void quickSort(int[] arr, int left, int right)
    {
        int index = partition(arr, left, right);
        if (left < (index - 1)) {
                ;
        }
        quickSort(arr, left, index - 1);
        if (index < right) {
                quickSort(arr, index, right);
        }
    }
}

Look how indention help in reading code..

Your problem is that testScan1 needs a return type, even if that type is void.

IIRC, I don't think you can print an array and see all the values, like you can in Python or Scala. You'll have to loop through the array to print the values:

for (int i = 0; i < array.length; i++) {
   System.out.println(array[i]);
}

You have two problems. You need to define your array outside the try block. Like this:

 int[] array = new int[5];
 Scanner scan;
 try {
    scan = new Scanner(file);
 } catch (java.io.FileNotFoundException e) {
      //etc.

Even that only really works because you return in the exception block, otherwise the compiler would complain that it is possible that an exception was thrown and scan was never assigned.

To print, use System.out.println(java.util.Arrays.toString(array));

That will make it in a readable format. You will get some weird internal garbage (ok maybe that is harsh, but that is how I think of the default behavior) if you just print the toString() value of the array (which is what happens if you just pass it to the println method).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top