0

I've got lines set up something like this:

A a-type_value
B b-type_value
C c-type_value
B b-type_value
C c-type_value
C c-type_value
A a-type_value

My Java code is reading lines like those from a file like so:

BufferedReader reader = new BufferedReader(new FileReader("C:\\Path\\to\\File.txt"));
        try {
          while (true) {
            String line = reader.readLine();
            if (line == null) break;
            String[] fields = line.split("\t");
            LineProcess(fields);
          }
        } finally {
          reader.close();

The LineProcess() method looks like this:

public static void LineProcess(String[] input)
    {
        int result = 0;
        String datetime = null;
        System.out.println(input[0]);
        if (input[0] == "A") {
            result = Integer.parseInt(input[1]);
            System.out.println(result);
        }
        if (input[0] == "B") {
            bvalue = input[1] + input[2];
            System.out.println(bvalue);
        }
        if (input[0] == "C") {

            System.out.println(input[1]);
        }
        //System.out.println(input.length);
        //for(int i = 0; i < input.length; i++) 
        //  System.out.print(input[i] + " ");
        //System.out.println("");
    }

Although there are A's, B's, and C's in my input, the if statements are never catching them. I'm sure I'm making a simple mistake.

Thanks

BWONG
  • 153
  • 1
  • 2
  • 9

2 Answers2

2

Use the equals() method to compare strings. I.e.

if (input[0].equals("A")) {
...
isak gilbert
  • 2,541
  • 1
  • 14
  • 11
1

You're splitting the string on the tab character. It doesn't look like that's the delimiter in your input file.

Additionally, you need a second backslash in the argument to String.split("\t").

As suggested by braj in a comment, you should use:

line.split("\\s+");

which splits on one or one or more spaces.

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101