Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays 1

Status
Not open for further replies.

Dins19

Programmer
Aug 15, 2001
6
GB
Hi all

I would really appreciate anyone who could help me. I am trying to convert the following string "A:B - C:D --> L" into a substring "(A==B) && (C==D)".

However the output I am getting from the program below is "A== &&C==". How could i change it??

class Conversion{

public static void main(String[] args){

String r = "A:B - C:D --> L";

// Changing the delimiter '-' to '&&'
int index = 0;

while((index = r.indexOf("-")) != -1) {
char[] c1 = r.toCharArray();
char[] c2 = new char[c1.length + 1];

for(int i = 0; i < index; i++){
c2=c1;
}

c2[index+0] = '&';
c2[index+1] = '&';

r = new String(c2);
}

Changing the delimiter ':' to '=='
index = 0;

while((index = r.indexOf(&quot;:&quot;)) != -1) {
char[] c1 = r.toCharArray();
char[] c2 = new char[c1.length + 1];

for(int i = 0; i < index; i++){
c2 = c1;
}

c2[index+0] = '=';
c2[index+1] = '=';

r = new String(c2);
}
System.out.println(r);
}
}
 
You can take a look at this:-

// ConversionDemo.java

public class ConversionDemo
{
public static void main(String[] args)
{
int index = 0;
String a = &quot;A:B - C:D --> L&quot;;

while ((index = a.indexOf(&quot;:&quot;)) != -1)
{
String temp = &quot;&quot;;
if (index == 0)
a = &quot;(==&quot; + a.substring(index+1,a.indexOf(&quot; &quot;)) + &quot;)&quot; + a.substring(a.indexOf(&quot; &quot;)+1);
else if (index == a.length()-1)
a = a.substring(0,a.lastIndexOf(&quot; &quot;)) + &quot;(&quot; + a.substring(a.lastIndexOf(&quot; &quot;)+1,index) + &quot;==&quot; + a.substring(index+1) + &quot;)&quot;;
else
{
if (a.indexOf(&quot; &quot;) > index)
a = &quot;(&quot; + a.substring(0,index) + &quot;==&quot; + a.substring(index+1,a.indexOf(&quot; &quot;)) + &quot;)&quot; + a.substring(a.indexOf(&quot; &quot;)+1);
else
{
if (a.indexOf(&quot; &quot;) != -1)
{
a = a.substring(0,a.indexOf(&quot; &quot;)) + &quot;(&quot; + a.substring(a.indexOf(&quot; &quot;)+1,index) + &quot;==&quot; + a.substring(index+1);
a = a.substring(0,a.indexOf(&quot; &quot;)) + &quot;)&quot; + a.substring(a.indexOf(&quot; &quot;)+1);
}
else
a = &quot;(&quot; + a.substring(0,index) + &quot;==&quot; + a.substring(index+1) + &quot;)&quot;;
}
}
}

while ((index = a.indexOf(&quot;-&quot;)) != -1)
{
a = a.substring(0,index) + &quot;&&&quot; + a.substring(index+1);
}

System.out.println(a);
}
}

Leon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top