Java string arrays and while loop help

Code with C | Programming: Projects & Source Codes Forums JAVA Programming Java string arrays and while loop help

This topic contains 7 replies, has 1 voice, and was last updated by  Adelaid 2 years, 5 months ago.

  • Author
  • #11138

    I have some problems with array.

    So first of all,
    This program should end, when you write “end” , otherwise should keep all words, and when it ends, Ask for type of formatting (Part with format i know).

    I can’t get to work this first part, with keeping words. When i write “end” it will terminate properly, but when i write something else, i’ve got error ;x
    Any tips, what i should fix ? or i should use another argument in while ?

  • #11139


    I’ve done some modifications and here is the code

    However there is a small caveat to the above code. When printing, it prints all the values in the array including empty or null strings.

  • #11140


    It stuck again.

    Code is working, but not that way i want.

    It's shows properly words:

    null (i guess it's that word "end" right, ) Any tips how to replace that with "end" ?
    Or somehow i need to assign that "end" to end of array ?

    And the second question.

    How should code look if i want to have words like that:

    c a t s d o g s etc. Smile (Here i have only one thought, that i need somehow add space between words ;x)

    Null is because of that "string k = "end" ;d maybe...

  • #11141


    Question 1:
    Difficult without knowing what “i” is in lenght = i + 1; I don’t really know why you even do this step as it makes the first step redundant (int lenght=words.length)
    If I understand correctly you want to do what varuint suggests and loop while words[i].compareToIgnoreCase(“end”) != 0, this should avoid the null…

    Question 2:
    You could use toCharArray on words[i] then loop to print it with spaces between individual characters…

  • #11142


    To eliminate the nulls you can count the number of objects that aren’t null, assign that to a new String array and copy the Strings into the new array. You could also use an ArrayList object instead of a String array, with which you don’t need to specify a size in advance.

    You could also do a null/blank check before printing out the words:

  • #11143


    There is no method named empty() for a String object.

    Here is the code that prints all the received words also ignoring nulls now.

    For the second part of the question, are you trying to print all the words without any spaces or printing all words in the same line with a space between each?

  • #11144


    Finally get it, before your reply Smile. But i made few fixes and it’s working like a charm.

    And final question !

    If want to format text like that (spaced) in the same line.
    I.E. c a t d o g c a t etc.

    And first thought is, chaAt, for loop and add space between letters ? Am i thinking right ? Smile

    Somehow i managed to do that:

  • #11145


    Here is my code in the format you’re looking for

You must be logged in to reply to this topic.