*
The moose likes Beginning Java and the fly likes string code problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string code problem" Watch "string code problem" New topic
Author

string code problem

ritesh srivastav
Greenhorn

Joined: Nov 30, 2012
Posts: 8
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

Joined: May 03, 2008
Posts: 4531
    
    5

Moved from JavaFX. ritesh srivastav, please do learn to UseCodeTags <- link


luck, db
There are no new questions, but there may be new answers.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18763
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string code problem