I’m having a problem with proper printing from an array.
In IF(A),
That should format all input letters in that way.
c a t d o g etc etc
But, the following only formats the last word I write. I’m only guessing why, and don’t know how to fix it.
That part is marked with :**”
String type;
String word;
String words[]=new String[100];
int i=0;
String k="end";
System.out.println("Type word: ");
word=sc.next();
while(wyraz.compareToIgnoreCase(k)!=0){
words[i]=wyraz;
i=i+1;
word=sc.next();
}
words[i]=k;
System.out.println("Choose type A,B,C:");
type=sc.next();
**if(type.equals("A")){
int length=word.length();
for(int z=0;z<length;z++){
System.out.print(words[z]=""+word.charAt(z));
System.out.print(" ");
}**
if(type.equals("B")){
int length=i+1;
for(int x=0;x<length;x++){
System.out.print(words[x]);
System.out.println();
}
}
if(type.equals("C")){
int length=i+1;
int j;
for(i=0; i<length; i++) {
for(j=0; j<i; j++) {
System.out.print(" ");
System.out.print(words[j]);
System.out.println();
}
}
}
From what I can tell, you are not iterating through all indexes
This should, if you have e words (eg
"cat","dog","end"), output as"c a t d o g". If you want to also have the “e n d”, changeto
I will also say that since you’re using an array the user can end prematurely (leaving the rest of the indexes
null, you should consider using anArrayList <String>instead.