• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string code problem

 
ritesh srivastav
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have string s = "r88i66t76s76h43";

i want output first three character then leave two character then again three
like
r88
6t7
76h

i had tried tht code but its nt give proper output(my taken string is differ from upper string..)


public class AlterStringDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "r98i345e45s77h9";
char ch[]= s.toCharArray();
for(int i=0;i<ch.length;i++){
for(int j=1;j<4;j++){


System.out.print(ch[i]);
continue;
}for(int k=1;k<3;k++){
System.out.println("");
}

}

}

}
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved from JavaFX. ritesh srivastav, please do learn to UseCodeTags <- link

 
Henry Wong
author
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ritesh srivastav wrote:i have string s = "r88i66t76s76h43";

i want output first three character then leave two character then again three
like
r88
6t7
76h

i had tried tht code but its nt give proper output(my taken string is differ from upper string..)


public class AlterStringDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "r98i345e45s77h9";
char ch[]= s.toCharArray();
for(int i=0;i<ch.length;i++){
for(int j=1;j<4;j++){


System.out.print(ch[i]);
continue;
}for(int k=1;k<3;k++){
System.out.println("");
}

}

}

}


My normal response would be to print out what the loop values -- and you should easily see what is going on. However, since you already did that, perhaps you should print out the loop indexes as well.

Another option is to play computer, and follow your program using paper and a pencil -- it will quickly become clear why the program is outputting the results you see.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic