因此,我一直试图将文件中的数据读取到数组中,并使用冒泡排序来组织数据。我需要一种用于读取文件和冒泡排序的方法。除了我的代码永远不会到达冒泡排序之外,我不知道如何将数据读入冒泡排序。这是我的代码:
{
Scanner in = new Scanner (System.in);
//Error trap
while (true)
{
System.out.println ("Which sorting method would you like to use? (Type bubble for Bubble Sort or insert for Insertion Sort)");
String sort = in.nextLine ();
if (sort.equalsIgnoreCase ("bubble"))
{
bubble (args, args, args, args, args);
}
if (sort.equalsIgnoreCase ("insert"))
{
insert ();
}
else
{
System.out.println ("Please enter the correct data.");
}
}
}
public static void readfile () throws IOException
{
//Declaring variables
String fileName, fnameTemp, lnameTemp, cityTemp, ageTemp, emailTemp;
int howMany = 0;
Scanner in = new Scanner (System.in);
System.out.println ("Enter the file name:");
fileName = in.nextLine ();
BufferedReader input;
input = new BufferedReader (new FileReader (fileName));
fnameTemp = input.readLine ();
lnameTemp = input.readLine ();
cityTemp = input.readLine ();
ageTemp = input.readLine ();
emailTemp = input.readLine ();
//Reading data until end of the file to determine the number of records
while (fnameTemp!= null)
{
howMany++;
fnameTemp = input.readLine ();
lnameTemp = input.readLine ();
cityTemp = input.readLine ();
ageTemp = input.readLine ();
emailTemp = input.readLine ();
}
input.close ();
input = new BufferedReader (new FileReader (fileName));
String fname [] = new String [howMany];
String lname [] = new String [howMany];
String city [] = new String [howMany];
String age [] = new String [howMany];
String email [] = new String [howMany];
for (int count = 0; count < howMany; count++)
{
fname [count] = input.readLine ();
lname [count] = input.readLine ();
city [count] = input.readLine ();
age [count] = input.readLine ();
email [count] = input.readLine ();
}
input.close ();
for (int count = 0; count < howMany; count++)
{
//System.out.println (fname [count]);
//System.out.println (lname [count]);
//System.out.println (city [count]);
//System.out.println (age [count]);
//System.out.println (email [count]);
}
}
public static void bubble (String fname [], String lname [], String city [], String age [], String email []) throws IOException
{
String temp, temp2, temp3, temp4, temp5;
int howMany = 0;
readfile ();
Scanner in = new Scanner (System.in);
System.out.println ("Which item would you like to sort?");
System.out.println ("Type firstname, lastname, city, age, or email to choose.");
String item = in.nextLine ();
if (item.equalsIgnoreCase ("firstname"))
{
for (int i = 1; i <= howMany; i++)
{
for (int j = 0; j < howMany; j++)
{
if (fname [j].compareToIgnoreCase (fname [j + 1]) > 0)
{
temp = fname [j];
fname [j] = fname [j + 1];
fname [j + 1] = temp;
temp2 = lname [j];
lname [j] = lname [j + 1];
lname [j + 1] = temp2;
temp3 = city [j];
city [j] = city [j + 1];
city [j + 1] = temp3;
temp4 = age [j];
age [j] = age [j + 1];
age [j + 1] = temp4;
temp5 = email [j];
email [j] = email [j + 1];
email [j + 1] = temp5;
System.out.println (fname [i]);
System.out.println (lname [i]);
System.out.println (city [i]);
System.out.println (age [i]);
System.out.println (email [i]);
System.out.println ("Sorted");
}
}
}
}
仅是一个注释,它永远不会过去(int i = 1; i <= howMany; i ++)
我的文本文件在每一行上都有一条数据,从名字,姓氏,城市,年龄和电子邮件开始。
任何帮助将不胜感激。谢谢
首先,它没有超过该点的原因是因为您将howMany初始化为0,并且从不更改它,因此for循环将i初始化为1,该值大于howMany,然后再不执行。我也有些困惑,因为您说您要使用文件中的数据,但主要是将命令行参数传递给函数。修复
我个人也会使用.csv文件来存储信息,因为我认为它们更易于使用。
不管怎样,这里有一些代码(使用.csv,但无论如何修改都非常容易),它将获取文件数据并将其传递给冒泡排序。我没有对排序方法做任何事情:
public static void main(String[] args) {
try {
String[] users = readFile("C:\\Users\\vikin\\OneDrive\\Desktop\\data.csv");
for(String s : users) {
System.out.println(s);
}
bubble(users);
} catch(IOException e) {
e.printStackTrace();
}
}
public static String[] readFile(String fileName) throws IOException {
Scanner scanner = new Scanner(new File(fileName));
ArrayList<String> data = new ArrayList<>();
while(scanner.hasNext()) {
//String[] split = scanner.nextLine().split(",");
//Collections.addAll(data, split);
data.add(scanner.nextLine());
}
String[] users = new String[data.size()];
for(int i = 0; i < data.size(); i++) {
users[i] = data.get(i);
}
return users;
}
public static void bubble (String[] data) throws IOException
{
String temp, temp2, temp3, temp4, temp5;
Scanner in = new Scanner (System.in);
String[] fname = new String[data.length];
String[] lname = new String[data.length];
String[] city = new String[data.length];
String[] age = new String[data.length];
String[] email = new String[data.length];
for(int i = 0; i < data.length; i++) {
String[] s = data[i].split(",");
fname[i] = s[0];
lname[i] = s[1];
city[i] = s[2];
age[i] = s[3];
email[i] = s[4];
}
//rest of sort here
}