2015-02-24 76 views
-3

我很难学习文件行处理。我知道如何通过while循环和.hasNextLine来读取文件的行,并且如果它包含我正在搜索的值,我甚至可以打印整行。我只是不明白如何在文本文件的特定行中获取单个值(字符串,整型,双精度)并对它们进行排序,然后按特定顺序打印出我想要的值。我的教授刚刚通过我们的教科书中的这一章,我们有一个测试即将到来,任何有助于弄清楚这一点将非常感激(甚至开放视频讲座/教程链接)Java文件行处理(基础知识)

我会扔在一起的做法下面的问题给我举例说明我的意思。

一个文本文件,列出了得分的学生在过去的3次测试,如:

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

现在,说我要输出的最高分数与他们按字母顺序命名每个孩子。我该怎么做呢?我在本周晚些时候进行的测试不允许使用阵列或任何奇特的实用程序,我们预计只能使用扫描器和循环。

预期输出:

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"); 
     } 
    } 

我不知道如果我在正确的轨道上,或者如果我错过了一件令人难以置信的明显东西,那将会照亮一切,并让我成为文件处理的上帝。所有的社区智慧都表示赞赏

+0

看起来文本输入将会根据什么数据在哪一行以及它们之间有多少空间而被随机格式化。这是否准确? – childofsoong 2015-02-24 01:59:17

+0

是的,它是随机格式化的。每个数据值之间的单个空间,然后是换行符后的随机间距。根据我们在课堂上所做的,我只是把它放在头顶上。当数据到处都是这样的时候,是否无法获得所需的输出? – zodian 2015-02-24 02:34:41

+0

好奇为什么这个问题得到3 downvotes? – 2015-02-24 02:36:29

回答

1

“我走在本周晚些时候将不允许数组或任何花哨的实用程序测试中,我们将有望只使用扫描仪和循环真的。”

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

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

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

注意:这个例子只是对输入进行排序,它没有找到最高的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 
0

请尝试以下方法。它通过分析文件正确&能够打印出所需的输出(未排序):

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循环了。首先将&存储到数组中,然后应用任何选择的排序算法0​​然后打印。现在您可以正确读取数据,您应该可以继续。

+0

既然他说他不会被允许使用数组,他将不得不使用嵌套的if语句来硬编码。 – childofsoong 2015-02-24 07:49:21

+0

@soong哎呀。忘记那部分。我猜乔尼的答案现在会做。 – Vineet 2015-02-25 01:47:10