• Java string arrays and while loop help

    LowellFeierabend Member

    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 ?

    Exception in thread "main" java.lang.NullPointerException
       at lab13zad3.main(lab13zad3.java:16)
    
    import java.util.Scanner;
    public class lab13zad3{
        public static void main(String[] args) {
           Scanner sc= new Scanner(System.in);
           
           String word;
           String words[]=new String[100];
           int i=0;
           
           String k="end";
           System.out.println("Write some words: ");
           word=sc.next();
           while(word.compareToIgnoreCase(k)!=0){
           i=i+1;
           word=words[i];
           }
    
  • codewithc
    CWC Keymaster

    I’ve done some modifications and here is the code

    import java.util.Scanner;
    public class lab13zad3{
        public static void main(String[] args) {
           Scanner sc= new Scanner(System.in);
           String word;
           String words[]=new String[100];
           int i=0;
           
           String k="end";
           System.out.println("Write some words: ");
           word=sc.next();
           while(word.compareToIgnoreCase(k)!=0)
           {
             words[i] = word;
             i=i+1;
             word=sc.next();
           }
           
           
          for(int j=0;j
    

    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.

  • SapnaVishwas Member

    It stuck again.

    if(type.equals("A")){
           int lenght=words.length;
           lenght=i+1;
           i=0;
           while(i
    

    Code is working, but not that way i want.

    It's shows properly words:

    Cat
    Cats
    blabla
    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...

  • codewithc
    CWC Keymaster

    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…

  • Abhey Member

    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:

    if (words[i]!=null && !words[i].empty()){
    ...
    }
    
  • SapnaVishwas Member

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

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

    import java.util.Scanner;
    public class lab13zad3{
        public static void main(String[] args) {
           Scanner sc= new Scanner(System.in);
           
           String word;
           String words[]=new String[100];
           int i=0;
           
           String k="end";
           System.out.println("Write some words: ");
           word=sc.next();
           
           //Add words to the array
           while(word.compareToIgnoreCase(k)!=0)
           {
             words[i] = word;
             i=i+1;
             word=sc.next();
           }
           
           //Add "end" after receiving all the words
          words[i] = k;
          System.out.println("The received words are:");
          for(int j=0;j 0)
               System.out.println(words[j]);
          }
        }
    }           
    

    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?

  • Amit Member

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

    // This IF format text like that
    cat
    cat
    cat
    cat
    //
    if(typ.equals("B")){
           int length=i+1;
           
           
           for(int x=0;x
    

    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:

    if(typ.equals("A")){
               int length=words.length();
               
               for(int z=0;z
        
  • Adelaid Member

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

    import java.util.Scanner;
    
    public class lab13zad3{
        public static void main(String[] args) {
           Scanner sc= new Scanner(System.in);
           
           String word;
           String words[]=new String[100];
           int i=0;
           
           String k="end";
           System.out.println("Write some words: ");
           word=sc.next();
           
           //Add words to the array
           while(word.compareToIgnoreCase(k)!=0)
           {
             words[i] = word;
             i=i+1;
             word=sc.next();
           }
           
           //Add "end" after receiving all the words
          words[i] = k;
          System.out.println("The received words are:");
          for(int j=0;j 0)
               System.out.println(words[j]);
          }
         
          System.out.println("Word with a space between each character");
          for(String w:words)
          {
            if(w != null && w.length() > 0)
            {
              char temp[] = w.toCharArray();
              for(int c=0;c
        
Viewing 7 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish