1

Say I have a .txt file that has information being split by a comma as such:

IN,Indiana,6634007

While this is a snippet which accesses that file and splits it:

for(int i=0; i < count; i++) {
    line = bufferedReader2.readLine();
    String space[] = line.split(",");
    String abb = space[0];
    String nme = space[1];
    int pop = Integer.parseInt(space[2]);
    states[i] = new State(abb, nme, pop);
    }

The purpose of that was so that all the information in the txt file could be accessed, so for example this code would print exactly whats present on the .txt file:

System.out.println(states[0]);

would print:

IN,Indiana,6634007

My question is, how would I have it so that I can access the specific part of the array as in how would I print lets say just the name "Indiana" or the population "6634007"?

P.S I'm sorry if the title of my question did not make sense, I did not exactly know how to word it.

3
  • If you have a comma separated file, then you have something close to a CSV file, please check apache commons csv and use it, I believe will help you a lot. Commented Aug 31, 2018 at 4:17
  • You can split it again states[0].split(",") Commented Aug 31, 2018 at 4:18
  • Create a class with fields corresponding to the items. Commented Aug 31, 2018 at 4:23

3 Answers 3

3

Somewhere, you have a class called State. states is an Array of this class. So you can add a getter to State:

public int getPop() {
    return pop;
}

And call it on your Object like this:

System.out.println(states[0].getPop());

as states[0] is simply a State object.

Add more getters to access different fields.

Sign up to request clarification or add additional context in comments.

Comments

0

if you just want to print every single line, you can try this like below:

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

    BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
    String line = null;
    List<String> list = new ArrayList<String>();
    while((line = reader.readLine()) != null ) {
        list.add(line);

    }

    System.out.println(list.get(0));

    // TODO realease resources
}

1 Comment

No, OP is already able to print whole lines. But he wants to print only the third value (if you split the line at ,)
0

From your question what i can realise is, you are using State class to store the information. In such case, check the state class where the first parameter value is stored. Later to print the corresponding information, access its object variable as SapuSeven mentioned.

For eg.

public class State{
     public String a;
     public String b;
     public int c;
     public State(String x, String y, int z){
       a=x;
       b=y;
       c=z;
     } 
 }

now u can access like

System.out.println(states[0].b);

for printing the name of city

OR

you can simply print the value using index like this

System.out.println(states[0].split(",")[2]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.