Java文件行处理(基础知识)

时间:2015-02-24 01:55:26

标签: java file io line

我很难学习文件行处理。我理解如何通过while循环和.hasNextLine读取文件的行,如果它包含我正在搜索的值,我甚至可以打印整行。我只是不明白如何在文本文件的特定行中获取单个值(字符串,整数,双精度)并对它们进行排序,然后按特定顺序打印出我想要的值。我的教授刚刚在我们的教科书中加快了这一章,我们即将进行测试,任何帮助解决这个问题都会非常感激(甚至对视频讲座/教程链接开放)

我将把下面的练习题放在一起,给出我的意思。

文本文件列出了最近3次测试的学生成绩,例如:

Nick 85 90 76 Hannah 86 91 66 Joe
           22 35 100
   Zak 100
 40 80

现在,假设我想按字母顺序输出每个孩子的前2个分数。我该怎么做呢?我本周晚些时候进行的测试不允许使用数组或任何花哨的实用程序,我们预计只会使用扫描仪和循环。

预期产出:

Hannah: 91, 86
Nick: 90, 85
Joe: 100, 35
Zak: 100, 80

我现在正在接近这样的问题:

    Scanner exampleFile = new Scanner(new File("file.txt"));
    String name;

    while(exampleFile.hasNextLine()){
        name = exampleFile.nextLine();
        Scanner linescan = new Scanner(name);
        if(linescan.hasNext()){
            int a = linescan.nextInt();
            int b = linescan.nextInt();
            int c = linescan.nextInt();
            int a1 = Math.max(a, b);
            int a2 = Math.max(a, c);

            System.out.println(linescan.next() + ": " + Math.max(a1, a2) + ", " + "middle value idk how to get");
        }
    }

我不确定自己是否走在正确的轨道上,或者我是否遗漏了一些非常明显的东西,这些东西会照亮一切,让我成为一个文件线处理神。所有社区智慧都赞赏。

2 个答案:

答案 0 :(得分:1)

  

“我本周晚些时候进行的测试不允许使用数组或任何花哨的工具,我们预计只会使用扫描器和循环。”

没有数据结构的排序会适得其反,但我离题了。

在这种情况下,我认为对数据进行排序的唯一方法是:一次读取一行数据,修剪该行,然后将其附加到带有尾随空格的String。然后你将不得不想出一些爬行说String的算法,然后比较并交换它sub-strings直到它被排序。从经验来看,这不是最愉快的活动。

这是我刚刚输入的一个工作示例。它使用一个Scanner进行文件输入,String s。 String不是数组,所以这很好。我尝试通过代码中的注释来解释事情,但如果您需要更好的解释,请在本回答的评论中告诉我。

注意:这个例子只是对输入进行排序,它没有找到最高的2个等级,但是一旦输入已经排序,这应该很容易。只需将已排序的String传递给Scanner,解析一些数据即可。


<强> Driver.java:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Driver {

    public static void main(String[] args) {
        Scanner in = null;
        String inputStr = "";

        try {
            // open the file "xin.txt" to retrieve input
            in = new Scanner(new File("xin.txt"));
        } catch(FileNotFoundException e) {
            System.out.println(e.getMessage());
            System.exit(0);
        }

        // read the data into a one line string
        while(in.hasNext()) {
            inputStr += in.nextLine().trim() + " ";
        }
        System.out.println("[ORIGINAL]\n" + inputStr + "\n");

        System.out.println("[Start Sorting]");
        inputStr = sortString(inputStr);
        System.out.println("[Done Sorting]\n");

        System.out.println("[Sorted String]\n" + inputStr);
    }

    private static String sortString(String orig) {
        String one = "";
        String two = "";
        int oneStart = 0; // starting index of string to compare with
        int twoStart = 0; // starting index of string to compare against
        int oneStop = 0; // ending index of string to compare with
        int twoStop = 0; // ending index of string to compare against
        int oneTot = 0; // length of string one plus the grades
        int twoTot = 0; // length of string two plus the grades

        int cp = 0; // current position (index)

        boolean onOne = false; // current position is on one
        boolean onTwo = false; // current position is on two
        boolean passOne = false; // passed string one
        boolean passTwo = false; // passed string two


        boolean notDone = true; // control boolean

        while(notDone) {
            if(cp >= orig.length() || isAtoZ(orig.charAt(cp))) {
                if(!onOne && !passOne) {
                    oneStart = cp;
                    onOne = true;
                } else if(passOne && (!onTwo && !passTwo)) {
                    twoStart = cp;
                    onTwo = true;

                    // total length of the first string is the current position
                    // minus the start of the first string
                    oneTot = cp - oneStart;
                } else if(passTwo) {
                    // total length of the second string is the current position
                    // minus the start of the second string
                    twoTot = cp - twoStart;

                    one = orig.substring(oneStart, oneStop);
                    two = orig.substring(twoStart, twoStop);

                    // output the results of the comparing
                    System.out.println(orig);
                    System.out.println("Comparing: " + one + " to " + two);
                    System.out.println("Result: " + one.compareTo(two) + "\n");



                    // if the first string is alphabetically larger, then swap
                    // and restart sort,else continue to the next comparison
                    if(one.compareTo(two) > 0) {
                        orig = rangeSwap(oneStart, oneStart + oneTot,
                        twoStart, twoStart + twoTot, orig);
                        onOne = false;
                        onTwo = false;
                        passOne = false;
                        passTwo = false;

                        cp = -1;
                    } else {
                        onOne = false;
                        onTwo = false;
                        passOne = false;
                        passTwo = false;

                        cp = twoStart - 1;
                    }

                }
            } else {
                if(onOne && !passOne) {
                    oneStop = cp;
                    passOne = true;
                    onOne = false;
                } else if(onTwo && !passTwo) {
                    twoStop = cp;
                    passTwo = true;
                    onTwo = false;
                }
            }

            // increment the current position by one
            cp = cp + 1;

            // the last string has no string to compare to so
            // set the control boolean
            if(cp >= (orig.length()-1) && (!passTwo)) {
                notDone = false;
            }
        }

        // return the sorted string
        return orig;
    }

    private static String rangeSwap(int ob, int oe, int tb, int te, String s) {
        String start = "";
        String x = "";
        String middle = "";
        String y = "";
        String end = "";

        start = s.substring(0, ob);
        x = s.substring(ob, oe);
        middle = s.substring(oe, tb);
        y = s.substring(tb, te);
        end = s.substring(te);

        return start + y + middle + x + end;
    }

    private static boolean isAtoZ(char what) {
        // ASCII character codes: A to Z = 65 to 90
        //                        a to z = 97 to 122
        return (((int)what >= 65 && (int)what <= 90) ||
        ((int)what >= 97 && (int)what <= 122 ));
    }

}

<强> xin.txt:

Nick 85 90 76 Hannah 86 91 66 Joe
           22 35 100
   Zak 100
 40 80

<强>输出:

[Original]
Nick 85 90 76 Hannah 86 91 66 Joe 22 35 100 Zak 100 40 80 

[Start Sorting]
Nick 85 90 76 Hannah 86 91 66 Joe 22 35 100 Zak 100 40 80 
Comparing: Nick to Hannah
Result: 6

Hannah 86 91 66 Nick 85 90 76 Joe 22 35 100 Zak 100 40 80 
Comparing: Hannah to Nick
Result: -6

Hannah 86 91 66 Nick 85 90 76 Joe 22 35 100 Zak 100 40 80 
Comparing: Nick to Joe
Result: 4

Hannah 86 91 66 Joe 22 35 100 Nick 85 90 76 Zak 100 40 80 
Comparing: Hannah to Joe
Result: -2

Hannah 86 91 66 Joe 22 35 100 Nick 85 90 76 Zak 100 40 80 
Comparing: Joe to Nick
Result: -4

Hannah 86 91 66 Joe 22 35 100 Nick 85 90 76 Zak 100 40 80 
Comparing: Nick to Zak
Result: -12

[Done Sorting]

[Sorted String]
Hannah 86 91 66 Joe 22 35 100 Nick 85 90 76 Zak 100 40 80 

答案 1 :(得分:0)

以下列方式尝试。它正确地解析文件&amp;能够打印所需的输出(未排序):

    Scanner exampleFile = new Scanner(new File("file"));
    while (exampleFile.hasNext()) {
        String name = exampleFile.next();
        int a = exampleFile.nextInt();
        int b = exampleFile.nextInt();
        int c = exampleFile.nextInt();
        int max = Math.max(a, b);
        max = Math.max(max, c);
        int min = Math.min(a, b);
        min = Math.min(min, c);
        System.out.println(name + ": " + max + ", " + (a > min && a < max ? a : (b > min && b < max ? b : c)));
    }
    exampleFile.close();

如果你想排序,那么你将不得不使用多个while循环。首先阅读&amp;存储到数组中,然后应用任何选择的排序算法&amp;然后打印。既然您可以正确读取数据,那么您应该可以继续了。