从文件读取的数据在冒泡排序方法中无法正常工作。

huangapple 未分类评论45阅读模式
英文:

Data Read From File Not Working in Bubble Sort Method

问题

以下是您提供的代码的翻译部分:

{
    Scanner in = new Scanner(System.in);
    
    // 错误处理
    while (true) {
        System.out.println("您想使用哪种排序方法?(输入 bubble 进行冒泡排序,输入 insert 进行插入排序)");
        String sort = in.nextLine();
        
        if (sort.equalsIgnoreCase("bubble")) {
            bubble(args, args, args, args, args);
        }
        if (sort.equalsIgnoreCase("insert")) {
            insert();
        }
        else {
            System.out.println("请输入正确的数据。");
        }
    }
}

public static void readfile() throws IOException {
    // 声明变量
    String fileName, fnameTemp, lnameTemp, cityTemp, ageTemp, emailTemp;
    int howMany = 0;
    
    Scanner in = new Scanner(System.in);
    
    System.out.println("请输入文件名:");
    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();
    
    // 读取数据直到文件末尾以确定记录数
    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("您想对哪个项目进行排序?");
    System.out.println("输入 firstname、lastname、city、age 或 email 进行选择。");
    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("已排序");
                }
            }
        }
    }
}

如果您有任何进一步的问题或需要进一步的帮助,请随时提问。

英文:

so I've been trying to read data from a file into arrays and use a bubble sort to organize the data. I need a method for both the file read and the bubble sort. I don't know how I would get the data to read into the bubble sort, in addition to the fact that my code never reaches the bubble sort. Here is my code:

{
	Scanner in = new Scanner (System.in);
	
	//Error trap
	while (true)
	{
		System.out.println (&quot;Which sorting method would you like to use? (Type bubble for Bubble Sort or insert for Insertion Sort)&quot;);
		String sort = in.nextLine ();
		
		if (sort.equalsIgnoreCase (&quot;bubble&quot;))
		{
			bubble (args, args, args, args, args);
		}
		if (sort.equalsIgnoreCase (&quot;insert&quot;))
		{
			insert ();
		}
		else
		{
			System.out.println (&quot;Please enter the correct data.&quot;);
		}
	}
}

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 (&quot;Enter the file name:&quot;);
	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 &lt; 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 &lt; 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 (&quot;Which item would you like to sort?&quot;);
	System.out.println (&quot;Type firstname, lastname, city, age, or email to choose.&quot;);
	String item = in.nextLine ();
	
	if (item.equalsIgnoreCase (&quot;firstname&quot;))
		{
			for (int i = 1; i &lt;= howMany; i++)
			{
				for (int j = 0; j &lt; howMany; j++)
				{
					if (fname [j].compareToIgnoreCase (fname [j + 1]) &gt; 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 (&quot;Sorted&quot;);
					}
				}
			}
		}

Just a note, it never gets past for (int i = 1; i <= howMany; i++)

My text file has a piece of data on every line going from first name, last name, city, age and email.

Any help would be greatly appreciated. Thanks

答案1

得分: 0

首先,不会通过该点的原因是因为您将 howMany 初始化为 0,然后从未更改它,因此 for 循环将 i 初始化为 1,而 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];
    }
    //排序的其余部分在这里
}
英文:

Well first off, the reason it doesn't get past that point is because you initialize howMany to 0 and never change it, so the for loop initializes i to 1, which is greater than howMany, and then never executes. I am also a bit confused because you say you want to use the data from the file, but then in main you pass the command line arguments to your function. To fix the

I would personally use a .csv file to store the info too as they are easier to work with in my opinion.

Anyways, here's some code (using .csv, but it would be pretty easy to modify for whatever), that will get the file data and pass it to bubble sort. I didn't do anything with the sorting method:

public static void main(String[] args) {
    try {
        String[] users = readFile(&quot;C:\\Users\\vikin\\OneDrive\\Desktop\\data.csv&quot;);
        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&lt;String&gt; data = new ArrayList&lt;&gt;();
    while(scanner.hasNext()) {
        //String[] split = scanner.nextLine().split(&quot;,&quot;);
        //Collections.addAll(data, split);
        data.add(scanner.nextLine());
    }

    String[] users = new String[data.size()];
    for(int i = 0; i &lt; 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 &lt; data.length; i++) {
        String[] s = data[i].split(&quot;,&quot;);
        fname[i] = s[0];
        lname[i] = s[1];
        city[i] = s[2];
        age[i] = s[3];
        email[i] = s[4];
    }
    //rest of sort here
}

huangapple
  • 本文由 发表于 2020年4月10日 05:45:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/61130715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定