2

基本的には、文字列内の要素を配列に残したまま、int 値と char 値から分離したいのですが、正直なところ、値を 2 つの異なる配列に分離する必要がある場合は、最後の部分は要件ではありません。idきれいにするためにそれらをまとめておくのが好きです。これは私の入力です:

5,4,A
6,3,A
8,7,B
7,6,B
5,2,A
9,7,B

今、私がこれまでに持っているコードは、一般的に私がやりたいことをしますが、完全ではありません

これが私のコードでなんとか生成した出力ですが、ここが私が立ち往生している場所です

54A
63A
87B
76B
52A
97B

ここが楽しい部分です。数値と文字値を取得してそれらを分離し、比較/数式で使用できるようにする必要があります。

基本的に私はこれが必要です

int 5, 4;
char 'A';

もちろん、それらが入っている配列に格納されています。これが私がこれまでに思いついたコードです。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;


public class dataminingp1 
{

    String[] data = new String[100];
    String line;

    public void readf() throws IOException 
    {

        FileReader fr = new FileReader("C:\\input.txt");
        BufferedReader br = new BufferedReader(fr);

        int i = 0;
        while ((line = br.readLine()) != null) 
        {
            data[i] = line;
            System.out.println(data[i]);
            i++;
        }
        br.close();
        System.out.println("Data length: "+data.length);

        String[][] root;

        List<String> lines = Files.readAllLines(Paths.get("input.txt"), StandardCharsets.UTF_8);

        root = new String[lines.size()][];

        lines.removeAll(Arrays.asList("", null)); // <- remove empty lines

        for(int a =0; a<lines.size(); a++)
        {
            root[a] = lines.get(a).split(" ");
        }

        String changedlines;
        for(int c = 0; c < lines.size(); c++)
        {
            changedlines = lines.get(c).replace(',', ' '); // remove all commas
            lines.set(c, changedlines);// Set the 0th index in the lines with the changedLine
            changedlines = lines.get(c).replaceAll(" ", ""); // remove all white/null spaces
            lines.set(c, changedlines);
            changedlines = lines.get(c).trim(); // remove all null spaces before and after the strings
            lines.set(c, changedlines);
            System.out.println(lines.get(c));

        }
    }

    public static void main(String[] args) throws IOException 
    {
        dataminingp1 sarray = new dataminingp1();
        sarray.readf();
    }   
}

私はJavaと信じられないほど遠く離れていないので、これをできるだけ簡単にやりたいと思っていますが、必要に応じて難しいプロセスで管理できるように学んでいます。どうぞよろしくお願いいたします。その単純さのおかげで、言語として Java が本当に好きになり始めています。

これは、混乱を解消するための私の質問への追加です。私がしたいのは、code/input.txt にある文字列配列に格納されている値を取得し、文字の場合は char、整数の場合は int など、さまざまなデータ型に解析することです。しかし、現在それを行う方法がわからないので、私が求めているのは、これらの値を異なる配列に分割することなく、これらの値をすべて同時に解析する方法はありますか?入力ファイルを調べて、すべての char とすべての int が開始する場所を正確に見つけるために、これで問題が少し解決することを願っています。

4

6 に答える 6

1

たぶん、このようなものが役立ちますか?

List<Integer> getIntsFromArray(String[] tokens) {
  List<Integer> ints = new ArrayList<Integer>();
  for (String token : tokens) {
    try {
      ints.add(Integer.parseInt(token));
    } catch (NumberFormatException nfe) {
      // ...
    }
  }
  return ints;
}

これは整数のみを取得しますが、必要なことを行うために少しハックすることもできます:p

于 2013-10-15T06:49:16.857 に答える
1
List<String> lines = Files.readAllLines(Paths.get("input.txt"), StandardCharsets.UTF_8);
String[][] root = new String[lines.size()][];

for (int a = 0; a < lines.size(); a++) {
    root[a] = lines.get(a).split(","); // Just changed the split condition to split on comma
}

Your root array now has all the data in the 2d array format where each row represents the each record/line from the input and each column has the data required(look below).

5   4   A   
6   3   A   
8   7   B   
7   6   B   
5   2   A   
9   7   B

You can now traverse the array where you know that first 2 columns of each row are the numbers you need and the last column is the character.

于 2013-10-15T06:50:38.457 に答える
1
 for(int c = 0; c < lines.size(); c++){
            String[] chars = lines.get(c).split(",");
            String changedLines = "int "+ chars[0] + ", " + chars[1] + ";\nchar '" + chars[0] + "';";
            lines.set(c, changedlines);
            System.out.println(lines.get(c));

        }
于 2013-10-15T06:42:33.647 に答える
1

入力形式がこのように標準化されていれば、非常に簡単です。それ以上指定しない限り (1 つの行に 3 つ以上の変数を含めることができる、または char を 3 番目だけでなく任意の列に含めることができるなど)、最も簡単な方法は次のとおりです。

    String line = "5,4,A";
    String[] array = line.split(",");
    int a = Integer.valueOf(array[0]);
    int b = Integer.valueOf(array[1]);
    char c = array[2].charAt(0);
于 2013-10-15T06:42:48.923 に答える