JAVA - 如何从扫描仪读取文件中检测“\ n”字符

时间:2014-04-26 14:17:56

标签: java

第一次发表海报,

我无法通过文本文件从扫描仪中读取返回字符。

正在读取的文本文件如下所示:

//test.txt start//

2

0 30 30 1

1 90 30 0

//test.txt end//

第一行:2(表示两点)

第二行:位置指数:0 xpos:30 ypos:30画线到位置1

第三行:位置指数:1 xpos:90 ypos:30绘制线到位置0

我知道代码更改应该在do / while中进行。

do {        
    edge[pNum].add(input.nextInt());
} while(input.hasNextInt());

代码的其余部分似乎按预期运行,但我似乎无法检测文本文件中的"\n"返回字符,以便将x,y值保存在位置数组和以下内容中ArrayList中的值,并为文本文件中的下一行开始处理。

以下是完整的代码:

public class PointReader extends JFrame {

    class GraphView extends JPanel{

    }

    public static void main(String[] args) throws Exception{

        String fileName;

        System.out.print("Input File Name: ");
        Scanner user = new Scanner( System.in ); 
        fileName = user.nextLine();

        java.io.File file = new java.io.File(fileName);

        Scanner input = new Scanner(file);

        int count = input.nextInt();

        int[][] position = new int[count][2];
        ArrayList[] edge = new ArrayList[count];

        for(int k = 0; k < edge.length; k++){
            edge[k] = new ArrayList<Integer>();
        }

        while(input.hasNextInt()){

            int pNum = input.nextInt();
            int xPos = input.nextInt();
            int yPos = input.nextInt();

            position[pNum][0] = xPos;
            position[pNum][1] = yPos;

            do{        
                edge[pNum].add(input.nextInt());
            }while(input.hasNextInt());
        }

        System.out.println(count);

        for(int i=0; i < count; i++){
            System.out.println(i + " " + position[i][0] + " " + position[i][1] + " ");

            for(int j = 0; j < edge[i].size(); j++){
                System.out.print(edge[i].get(j) + " ");
            }
        }
    }
}

我已经尝试了while(!(input.next().equals("\n")));,但仍未被发现。有任何想法吗?

3 个答案:

答案 0 :(得分:2)

如果要将文件分隔成行,最简单的方法可能是单独处理每一行。

while (input.hasNextLine())
{
    String line = input.nextLine();

    // DO STUFF WITH LINE...
}

所以你将每一行都放入String&#34; line&#34;并且可以用它做点什么,你不必自己处理分隔线。 但除此之外,我并不百分之百确定你要做什么。

答案 1 :(得分:1)

问题是新行字符被视为空格。

您可以手动设置扫描仪的分隔符:

scanner = new Scanner(...).useDelimiter(" "); //To use only space as a delimiter.

这会使换行显示为由scanner.next();

返回的令牌

您建议的代码更改现在应该有效。

答案 2 :(得分:0)

由于L33D的建议,我最终使用input.nextLine()获取剩余的值,然后通过另一个扫描仪抛出这些值:

Scanner input = new Scanner(file);

            int count = input.nextInt();

            int[][] position = new int[count][2];
            ArrayList[] edge = new ArrayList[count];

        for(int k = 0; k < edge.length; k++){
            edge[k] = new ArrayList<Integer>();
        }

         while(input.hasNext()){

            int pNum = input.nextInt();
            int xPos = input.nextInt();
            int yPos = input.nextInt();

            position[pNum][0] = xPos;
            position[pNum][1] = yPos;

            String extra = input.nextLine();
            Scanner linescan = new Scanner(extra);

            while(linescan.hasNextInt()){
                edge[pNum].add(linescan.nextInt());
            }
        }

然后代码显示文件:

// displays input read from file
        System.out.println("File input from file: ");
        System.out.println(count);

        for(int i=0; i < count; i++){
            System.out.print(i + " " + position[i][0] + " " + position[i][1] + " ");

            for(int j = 0; j < edge[i].size(); j++){
                System.out.print(edge[i].get(j) + " ");
            }

        System.out.println();
        }

最后图形代码如下:

// draws points, edges, and finally labels points
        int pointName = 0;

        for(int h = 0; h < count; h++){

            g.fillOval(position[h][0], position[h][1], 10, 10);

            for(int y = 0; y < edge[h].size(); y++){
                int ref = (int)edge[h].get(y);
                g.drawLine((position[h][0] + 5), (position[h][1] + 5), (position[ref][0] + 5), (position[ref][1]+ 5));
            }
            if(((h % 2) == 0)){
                g.drawString(" " + pointName+ " ", (position[h][0] - 15), (position[h][1] + 10));
            }
            else{
                g.drawString(" " + pointName + " ", (position[h][0] + 15), (position[h][1] + 10));
            }
            pointName++;
        }