0

I would like to say that I have an 1 day experience with java,therefore if its a bit easy question, sorry about that.

Now, I want to show my piece of code .

I have a string array like :

String [] attributeNames[4];

I've read 4 name each of which are 64 bytes from binary file and store them into this array. (attribute name contain null-ended string max 64 byte) And then I want to print my attributeNames array and their length however, If I write :

for(int i=0;i<attributeNames2.length;i++){
    System.out.println(
        attributeNames2[i] +
        " length: "+attributeNames2[i].length()
    );
}

I got only

pathway             
shortest_path
path_length
avg_neighbour_number

as a result . Length are not printed but If I write

for(int i=0;i<attributeNames2.length;i++){
    System.out.println(
        "length:"+attributeNames2[i].length() + 
        "names:"+ attributeNames2[i]
    );
}

I got

length: 64 pathway
length: 64 shortest_path
length: 64 path_length
length: 64 avg_neighbour_number

I wonder why ? Is there anyone to help me ?

8
  • 1
    In your first attempt you are creating a 2-Dimensional array.. Commented Jan 6, 2014 at 12:20
  • The output does not correspond to the code (which does not compile) - show you real code. Commented Jan 6, 2014 at 12:20
  • 1
    @TheLostMind does "string[] identifier[x];" compile to "string[][x] identifier"? (Genuine question as I've never written Java) Commented Jan 6, 2014 at 12:21
  • 1
    @TheGost Yes, It's just a hint to correct the line. :) As I wont take a step to correct it, because it may be your actual code. Commented Jan 6, 2014 at 12:22
  • 3
    @JamieTaylor - Yes... String [] attributeNames[] is same as String[][] attributeNames.. Commented Jan 6, 2014 at 12:25

1 Answer 1

1

Your loop seems ok, problem probably lies in a way you are creating the array. The right way is

String[] attributeNames2 = new String[4];
attributeNames2[0] = "pathway";
//and so on

Or you can create static array which seems to me sufficient in your case

String[] array = {"pathway", "shortest_path", "and so on"};
Sign up to request clarification or add additional context in comments.

1 Comment

attributeNames[0] will not be just "pathway", instead,since I read 64 bytes. from pathway to 64 bytes there will be whitespaces. I need to remove them I think

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.