0

ファイルによる入力形式

4

(6,7)(8,9)

5

(8,9)(6,7)

配列に格納されているすべての整数を取得するために、次のコードを作成しました

public static void main(String[] args) throws IOException
{   
   
    int n=0;
    int arr[]= new int[13];
    Scanner s = new Scanner(new File("data.txt"));
    s.useDelimiter("[,() ]{1,}");
    
    while(s.hasNextInt())
    {   arr[n]=s.nextInt();
       
        n++;    
    }
    s.nextLine();
    
    while(s.hasNextInt())
    {   
        arr[n]=s.nextInt();
        n++;            
    }
    s.nextLine();
    
    while(s.hasNextInt())
    {   
        arr[n]=s.nextInt();
        n++;
    }
    s.nextLine();

    while(s.hasNextInt())
    {   
        arr[n]=s.nextInt();
        n++;
    }    

    n=0;
    while(n<9)
    { 
        System.out.println(arr[n]);
        n++;
    }
}

ただし、括弧内に含まれる整数のみが得られます。

出力

6

7

8

9

8

9

6

7

望ましい出力-

4

6

7

8

9

5

8

9

6

7

配列に 4 と 5 を含めるにはどうすればよいですか?

4

1 に答える 1

1

ファイルの内容から Java 文字列を作成するにはどうすればよいですか? のようなものを使用します。内容を文字列として取得するには、次に使用します

String[] items = fileContent.split(",()[]{}\\s");
于 2012-09-14T06:49:34.147 に答える