package individual;
import java.io.*;
import java.util.*;
public class Individual{
public static void getContent(Scanner inp,String[] contents){
int i;
for(i=0;i<contents.length;i++)
contents[i]=inp.next();
public static void Sort(String[] contents){
int i,j;
int min;
String temp;
//for selection sort
for(i=0;i<contents.length-1;i++)
{
min=i;
for(j=i+1;j<contents.length;j++)
if(contents[j].compareTo(contents[min])<0)
min=j;
temp=contents[i];
contents[i]=contents[min];
contents[min]=temp;
}
}
public static void main(String[] args) throws FileNotFoundException{
String[] newAyat=new String[50];
//read From file that i save in drive
Scanner inFile=new Scanner(new FileReader("D:\\newFile.txt"));
getContent(inFile,newAyat);
Sort(newAyat);
}
}
}
所以当我运行程序时出现错误。输出不是我想要的。我的代码有问题吗?以及如何打印出排序结果?
这是我试图解决的问题: 根据列表相关,您必须编程可以显示以“a”,“b”,“c”等开头的单词列表。
答案 0 :(得分:0)
package textconcordance;
import java.util.*;
import java.io.*;
public class TextConcordance {
final static int SEN_LONG=8;
public static void getAyat(Scanner inp,String[] Sentence){
int i;
while(inp.hasNext()){
for(i=0;i<Sentence.length;i++)
Sentence[i]=inp.next();
}
}
//sort sentence
public static void sortSen(String[] Sentence){
int i,j;
int min;
String temp;
for(i=0;i<Sentence.length-1;i++)
{
min=i;
for(j=i+1;j<Sentence.length;j++)
if(Sentence[j].compareTo(Sentence[min])<0)
min=j;
temp=Sentence[i];
Sentence[i]=Sentence[min];
Sentence[min]=temp;
}
}
//for print
public static void print(String[] Sentence){
for(int i=0;i<Sentence.length;i++)
System.out.print(Sentence[i]+",");
}
public static void main(String[] args) throws FileNotFoundException{
String[] Sentence=new String[SEN_LONG];
Scanner inFile=new Scanner(new FileReader("D://Ghost.txt"));
getAyat(inFile,Sentence);
sortSen(Sentence);
inFile=null;
inFile=new Scanner(new FileReader("D://Story.txt"));
print(Sentence);
}
}