【发布时间】:2009-11-05 22:48:51
【问题描述】:
你们怎么了,
我正在尝试用 Java 编写一些代码,这些代码将从文件中读取数字(.txt 文件的每一行都有一个 #)将它们放入数组中,然后对数组运行快速排序。 Eclipse 显示了一些我遇到问题的红色。我的错误是用cmets标记的,是什么错误,如果有人能帮我把这个跑起来,谢谢大家!
-凯尔
好的,我更新了前两个答案,到目前为止,谢谢,但还有两个错误我不太理解。
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);
}
}
【问题讨论】:
-
我只是在回答这个问题,因为除了提问者之外,其他任何人都不会普遍感兴趣。没有冒犯的意思。祝你在课堂上好运。
标签: java arrays file quicksort