你们有什么事,
我正在尝试用Java编写一些代码来读取文件中的数字(每行.txt文件中的一个#)将它们放入一个数组中,然后对数组运行快速排序。 Eclipse正在显示一些我遇到问题的红色。我的错误标有评论,错误是什么,如果有人可以帮助我让它运行,谢谢大家!
-Kyle
好的,我更新了前两个答案,谢谢到目前为止,但还有两个错误我真的不理解。
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);
}
}
答案 0 :(得分:9)
任何时候你处理递归算法并且你得到堆栈溢出,这是因为你的算法没有明确定义的边缘情况会导致你的递归终止。 (或者你的输入太大了,但情况很少,而且不是这种情况。)
你应该看一下你的quickSort()
方法,看看是什么让它无限地调用它自己。想想看两个镜子的反射,其中反射从另一个反射反射,然后变成无限......这就是这里发生的事情。
此外,在Java语言中,建议始终使用大写字母开始您的类名。我会把你的班级命名为QuickSortHomework
或类似的东西。
此外,您可能希望了解if
语句在Java中的工作方式以及如何定义“块”。你有一个if
语句靠近分号和一对大括号,可能没有你想象的那样。
答案 1 :(得分:5)
老实说,我对这里的所有转发和转发都感到有点恼火。
这不是你想听到的,但我觉得你正在使用这个网站作为拐杖。你似乎没有花时间自己解决正在发生的事情。令人费解的过程,无论多么痛苦,都是真正的学习来源。
在这种情况下,如果你查看了那个错误意味着什么,然后你只是查看了你的quickSort()实现,我想你必须注意到它有一些非常明显的错误。
编辑:如果你正在思考“但是我确实试图解开它”......真的有助于包含在你的帖子里,“我想可能是这个,但那并没有工作,所以我想也许它可能......“等等。有一半时间你会突然意识到这个问题,而你正试图通过它这样说话。另一半,至少我们看到你正在努力。答案 2 :(得分:4)
一些错误:
public testScan1(String filename)
实际上没有任何返回类型,它也是从静态上下文调用的,但它不是静态的。它应该更改为public static void testScan1(String filename)
。file.hasNext()
的目的是什么?当然它不存在,因为它没有任何意义。我认为你的意思是scan.hasNext()
。array
,因为它是在try/catch
块内定义的,因此它仅存在于该范围内。在尝试之前移动定义。此外,尝试以更易读的方式缩进代码,因此很难找到错误。例如,为什么在}
调用之前有一个括号testScan
不属于我想要调用它的主要方法?
答案 3 :(得分:2)
正如我在上一个问题中告诉你的那样,无法找到数组,因为它仍在try块中。
然后,对于打印,您不能以有用的方式直接打印数组,您应该迭代每个元素并以下列方式打印它:
for (int i = 0; i < array.length; ++i)
System.out.println(array[i]+" ");
你在这里:
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);
}
}
}
看看indention如何帮助阅读代码..
答案 4 :(得分:1)
您的问题是testScan1需要返回类型,即使该类型无效。
答案 5 :(得分:1)
IIRC,我认为您不能打印数组并查看所有值,就像在Python或Scala中一样。您必须遍历数组才能打印值:
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
答案 6 :(得分:1)
你有两个问题。您需要在try块之外定义数组。像这样:
int[] array = new int[5];
Scanner scan;
try {
scan = new Scanner(file);
} catch (java.io.FileNotFoundException e) {
//etc.
即使这只是真的有效,因为你在异常块中返回,否则编译器会抱怨抛出异常并且从未分配scan
。
要进行打印,请使用System.out.println(java.util.Arrays.toString(array));
这将使其成为可读格式。如果只打印数组的toString()值,你会得到一些奇怪的内部垃圾(好吧可能是苛刻的,但这就是我想到的默认行为)(如果你把它传递给println会发生什么)法)。