views:

1352

answers:

7

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);
  }
 }
A: 

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

Myles
Ok, set return type to void, right?
Benzle
+4  A: 

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?

Jack
Great, thanks Jack, the "array[i]=scan.next() is still trouble, even after moving the declaration about the try block.
Benzle
Now the error is different, Scanner class works by specifying the kind of thing you want to read from stream. If you use plain scan.next() the return type is String, so you should convert it to the kind of number you want. In your case you can solve it with **array[i] = Integer.parseInt(scan.next())** or with **array[i] = scan.nextInt()**. They are they same thing.
Jack
Ok, thanks Jack, I've made a lot of changes and am going to repost, I think the question has changed enough that its new.
Benzle
+2  A: 

Hey, it's still me.

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..

Jack
Thank you Sir! My brackets are still off somewhere, can you help me with placement?: (errors on the first 3 together and the last ; in the snippet. }}} array[i] = Integer.parseInt(scan.next()); for (int i = 0; i < array.length; ++i) System.out.println(array[i]+" ");
Benzle
wait a second, I'll edit my answer
Jack
A: 

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]);
}
Kaleb Brasee
+1  A: 

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).

Yishai
+9  A: 

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.

Daniel Pryden
+1 for being so nice about it, which I couldn't muster.
Kevin Bourrillion
+5  A: 

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.

Kevin Bourrillion
Actually, I thought the question looked familiar, but after this I went back and looked at Benzle's previous questions -- last time around I gave this answer: http://stackoverflow.com/questions/1597831/starting-with-java-recursion-probably-an-easy-for-most/1597889#1597889 . Notably, I wrote: "I suspect that you simply don't understand what you're trying to do. That can't be fixed here -- you need to spend some more time studying until you really get it."
Daniel Pryden
Order his questions by newest to oldest and its even worse. He's asked this question 3 times in the last few hours.
Jherico
yeah, but then you still helped him. that's very cool of you. you've probably put more thought into his homework than he has (harsh, sorry).
Kevin Bourrillion
@Kevin: Well, I try to be a cool guy. But as it turns out I didn't put two and two together until *after* I had posted my answer. (Your answer came one minute after mine.) So yes, I'm a nice guy, but if I had realized who was asking the question I might not have been quite as nice.
Daniel Pryden