Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java issue

  • 27-09-2011 7:25pm
    #1
    Registered Users, Registered Users 2 Posts: 631 ✭✭✭


    I have a disagreement with a person on code below.

    String results[] = new String[21];

    I thought this was a string array but I have been told that it isn't and that
    its a single string 21 characters long, so what is it?

    and then I was printing the results from 20 students so I used a 'for' loop going from 1 to 21 and used the index 'i' to describe each student as in

    ..... ("Student %d %s",i,results) ......

    Anyway I was told that starting a 'for' loop not from zero was also wrong.
    What do ye guys think, am I completely wrong here, am I missing something?
    I program outputted as it was supposed to but was written 'wrong'.


Comments

  • Registered Users, Registered Users 2 Posts: 830 ✭✭✭Robby91


    I thought this was a string array but I have been told that it isn't and that
    its a single string 21 characters long, so what is it?
    Have you tried testing it out yourself? I don't know myself (my programming experience is rather limited) so I can't give a definite answer, but I can't see why it wouldn't be an array of Strings.
    Anyway I was told that starting a 'for' loop not from zero was also wrong
    From what I recall, Java arrays have a first index of [0], so going from [1] to [21] means that you'd completely miss the first index, and exceed your defined array size (since the array with 21 indices would go from [0] to [20] I believe).


  • Registered Users, Registered Users 2 Posts: 428 ✭✭Joneser


    You are right, you can indeed create an array of strings in the way you can with ints and chars. Also you should always start your "for loop" at 0 if you want to iterate through the entire array.


  • Registered Users, Registered Users 2 Posts: 631 ✭✭✭jimmyendless


    So that's not a string array, is it a char array?

    I didn't want to iterate from zero cause I didn't want a 'student 0' in my printed output and I didn't save any data in the first element of the array. The program worked perfectly but the logic I used was not appreciated.


  • Registered Users, Registered Users 2 Posts: 428 ✭✭Joneser




  • Registered Users, Registered Users 2 Posts: 490 ✭✭Chefburns


    So that's not a string array, is it a char array?

    I didn't want to iterate from zero cause I didn't want a 'student 0' in my printed output and I didn't save any data in the first element of the array. The program worked perfectly but the logic I used was not appreciated.

    It is an array of strings a char array would be char results[];


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,945 ✭✭✭Anima


    I didn't want to iterate from zero cause I didn't want a 'student 0' in my printed output and I didn't save any data in the first element of the array. The program worked perfectly but the logic I used was not appreciated.

    You're not indexing your results array though? So you'll be printing the same one for each iteration of the loop, unless I'm mistaken...

    [PHP]
    for( int i=0; i < results.length; ++i )
    System.out.printf( "Student %d %s", i+1, results );
    [/PHP]


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    String results[] = new String[21];
    Java is pretty flexible when it comes to the syntax of defining the array but there is a common standard used which is:
    String[] results = new String[21]
    
    Think about it as defining an array of strings called results rather than an array of results! As I said, your syntax will work fine, the other is the standard convention used for the syntax.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    I have a disagreement with a person on code below.

    String results[] = new String[21];

    I thought this was a string array but I have been told that it isn't and that
    its a single string 21 characters long, so what is it?

    and then I was printing the results from 20 students so I used a 'for' loop going from 1 to 21 and used the index 'i' to describe each student as in

    ..... ("Student %d %s",i,results) ......

    Anyway I was told that starting a 'for' loop not from zero was also wrong.
    What do ye guys think, am I completely wrong here, am I missing something?
    I program outputted as it was supposed to but was written 'wrong'.
    As everyone stated you are correct in saying it's an array of strings. To say it's an array of characters shows lack of knowledge.

    However, in practice, they were correct about starting at element 0, otherwise you'll miss the first one and the last index will be 20 in this case. (n-1).

    But if it was your intention to skip the first student then starting at 1 is correct but don't go beyond the array, but i'm sure you know that already as you'll get an exception.


  • Closed Accounts Posts: 577 ✭✭✭Galtee


    I have a disagreement with a person on code below.

    String results[] = new String[21];

    I thought this was a string array but I have been told that it isn't and that
    its a single string 21 characters long, so what is it?

    and then I was printing the results from 20 students so I used a 'for' loop going from 1 to 21 and used the index 'i' to describe each student as in

    ..... ("Student %d %s",i,results) ......

    Anyway I was told that starting a 'for' loop not from zero was also wrong.
    What do ye guys think, am I completely wrong here, am I missing something?
    I program outputted as it was supposed to but was written 'wrong'.

    Maybe the confusion lies in the fact that you can reference a string as an array of characters so to a new eye the above would look like you were specifying the length of the string?


Advertisement