• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with String  RSS feed

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys.

I have two strings:

String s1="1,2,3,";

String s2="1 1 1 1 1 1 ";

i want to remove whitespace in the s2 string.
and i want to get the values of s2 string based on s1 index(1,2,3) removing , in the s1 string..
The o/p should be like this

s1=1,2,3,4
s2=1 1 1 1 1 1 1 1
result is:1111
i want to display only first 4 characters (1,2,3,4) from the s2 string .

any suggestions?

Thanks and Regards
Sanny
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion would be that you post the code you've written so far & tell us the specifics of what errors you've encountered.
 
Sanny kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String step1="1,2,3,";
Sting score="1 1 1 0 1 1 1 1";
for(int i=0;i<step1.length();i++)

{
char c=score.charAt(i);
out.println(score.charAt(i));



}
here i m getting o/p is:111111
i want to ignore "," index from String score.to print only 111.

any suggestions?
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanny,

Am I right to say that the number of 1s to print from s2 depends on the numerical value of the last character in s1? So for:



You'll need to print out five 1s?

Come to think of it, this sounds pretty much like an assignment.

And don't think it really fits the intermediate level.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sanny kumar:

i want to ignore "," index from String score.to print only 111.


Sorry, I do not understand clearly what you would like to do. Do you want to print the numbers in the second string whose "index" is in the first one? So (changing to letters for clarity) given:

,

would you like the result to be ?

Anyway, I suggest you have a look at the String.split() method.

Regards,
Gabor
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And look at the String.replace() method for getting rid of whitespace in the middle of Strings.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And maybe have a look at java.util.StringTokenizer as well.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Java API:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
 
Ed Ward
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uhhhh, did I say StringTokenizer?
What I meant to say was don't do like I do and use StringTokenizer.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the string.replace to replace to replace the string with empty space wherever there are ","'s

then start at increment i by 2
for(int i=0; i<step1.Length(); i+=2){
char c = score.charAt(i);
out.println(c+"");
}

since or u can just use my code and dont do any replace

here is the iteration test

step1 = {1,2,3}
score = {1 1 1 0 1 1 1 1}

i=0
score.charAt(0) = 1

i+=2 = 2
score.charAt(2) = 1

i+=2 = 4
score.charAt(4) = 1

remember that empty space account as a position so u must increment by two

now you can also use the number in step 1. this will be used in case ur professor wants to mix the numbers around

step1 = "3,1,4"
score = "1 1 1 0 1 1 1 1"

use this code
int temp;
for(int i=0; i<step1.Length(); i+=2){
temp = (int)step1.charAt(i);
out.println(score.charAt(temp);
}
this will work the same way as b4 but will look at the integer value of the character in step1 and will then find its location in score.

hope that this was all helpful
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!