Tokenizer读取其他变量

时间:2013-10-22 15:35:37

标签: java tokenize

我从文件中读取有很大问题。我的文件中的数据看起来像

8
2 4 5 6
1 3 4 8
2 8
1 2 6 8
1 6
1 4 5 7 8
6 8
2 3 4 6 7

我希望将8读取到变量n(n表示2d表的大小:a [n] [n])并将值保留到表中。我写这个,但是当我从文件中删除第一行(值8)并且自己声明表的大小时代码工作正常

import java.io.*;
import java.util.StringTokenizer;
class dfs
{
private static PrintWriter zapis;
static void  przeszukiwanie_w_glab(int a[][], int m[], int i, int n)
{   
    int j;
    System.out.println("\t" + (i+1));
    zapis.print((i+1));
    m[i] = 1;
    for(j=0; j<n; j++)
        if(a[i][j]==1 && m[j]==0)
            przeszukiwanie_w_glab(a,m,j,n);
}  
public static void main(String args[]) throws IOException
    {
        zapis = new PrintWriter("Out0204.txt");
        int  n=8,i;
        int m[]= new int[n];
        int a[][] = new int[n][n];
        for (i=0; i<n; i++)
        {
            m[i] = 0;
        }

        BufferedReader in = new BufferedReader(new FileReader("In0204.txt"));
        String line;
        for (int curLine = 0; (line = in.readLine()) != null; curLine ++) {
            StringTokenizer tok = new StringTokenizer(line);
            while (tok.hasMoreElements())
                a[curLine][Integer.parseInt(tok.nextToken()) - 1] = 1;
        }
        in.close();
        System.out.println("\nKolejnosc odwiedzanych wierzcholkow : \n");
        for (i=0; i<n; i++)
            if (m[i]==0)
            {
                przeszukiwanie_w_glab(a,m,i,n);
            }
        zapis.close();          
    }

}

1 个答案:

答案 0 :(得分:0)

这不起作用吗?:

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

    BufferedReader in = new BufferedReader(new FileReader("In0204.txt"));
    int n = Integer.parseInt(in.readLine());

    zapis = new PrintWriter("Out0204.txt");
    int i;
    int m[]= new int[n];
    int a[][] = new int[n][n];
    for (i=0; i<n; i++)
    {
        m[i] = 0;
    }


    String line;
    for (int curLine = 0; (line = in.readLine()) != null; curLine ++) {
        StringTokenizer tok = new StringTokenizer(line);
        while (tok.hasMoreElements())
            a[curLine][Integer.parseInt(tok.nextToken()) - 1] = 1;
    }
    in.close();
    System.out.println("\nKolejnosc odwiedzanych wierzcholkow : \n");
    for (i=0; i<n; i++)
        if (m[i]==0)
        {
            przeszukiwanie_w_glab(a,m,i,n);
        }
    zapis.close();
}

更重要的是,请在编码时坚持使用Java命名约定。