substring?

M

Mike

Hi

The following program misses the last result.
May I ask why?

public class SubString
{
public static void main(String[] args)
{
String s1 = "Hello Java!!!";
System.out.println("s1 = " + s1);
char c1 = s1.charAt(0);
System.out.println("s1.toChar(7) = " + c1);

char a1[] = s1.toCharArray();
System.out.println("s1.toCharArray( ) = " + new String(a1));

char a2[] = new char[5];
s1.getChars(7, 11, a2, 0);
System.out.println("s1.getChars(7, 11, a2, 0) = " + new String(a2));

String s2 = s1.substring(7, 11);
System.out.println("s1.substring(7, 11 ) = " + s2); // No results
here...
}
}

Thank you in advance.
Mike
 
M

Mike

Hi

The following program misses the last result.
May I ask why?

public class SubString
{
public static void main(String[] args)
{
String s1 = "Hello Java!!!";
System.out.println("s1 = " + s1);
char c1 = s1.charAt(0);
System.out.println("s1.toChar(7) = " + c1);

char a1[] = s1.toCharArray();
System.out.println("s1.toCharArray( ) = " + new String(a1));

char a2[] = new char[5];
s1.getChars(7, 11, a2, 0);
System.out.println("s1.getChars(7, 11, a2, 0) = " + new String(a2));

String s2 = s1.substring(7, 11);
System.out.println("s1.substring(7, 11 ) = " + s2); // No results
here...
}

}

Thank you in advance.
Mike

Sorry. I use Jcreator.
 
A

Alex.From.Ohio.Java

The following program misses the last result.
May I ask why?
public class SubString
{
public static void main(String[] args)
{
String s1 = "Hello Java!!!";
System.out.println("s1 = " + s1);
char c1 = s1.charAt(0);
System.out.println("s1.toChar(7) = " + c1);
char a1[] = s1.toCharArray();
System.out.println("s1.toCharArray( ) = " + new String(a1));
char a2[] = new char[5];
s1.getChars(7, 11, a2, 0);
System.out.println("s1.getChars(7, 11, a2, 0) = " + new String(a2));
String s2 = s1.substring(7, 11);
System.out.println("s1.substring(7, 11 ) = " + s2); // No results
here...
}

Thank you in advance.
Mike

Sorry. I use Jcreator.
Probably this is the answer.
All other IDEs and javac/java produce perfectly valid result ;)

s1 = Hello Java!!!
s1.toChar(7) = H
s1.toCharArray( ) = Hello Java!!!
s1.getChars(7, 11, a2, 0) = ava!
s1.substring(7, 11 ) = ava!


Alex.
http://www.myjavaserver.com/~alexfromohio/
 
M

Mike

Hi
The following program misses the last result.
May I ask why?
public class SubString
{
public static void main(String[] args)
{
String s1 = "Hello Java!!!";
System.out.println("s1 = " + s1);
char c1 = s1.charAt(0);
System.out.println("s1.toChar(7) = " + c1);
char a1[] = s1.toCharArray();
System.out.println("s1.toCharArray( ) = " + new String(a1));
char a2[] = new char[5];
s1.getChars(7, 11, a2, 0);
System.out.println("s1.getChars(7, 11, a2, 0) = " + new String(a2));
String s2 = s1.substring(7, 11);
System.out.println("s1.substring(7, 11 ) = " + s2); // No results
here...
}
}
Thank you in advance.
Mike
Sorry. I use Jcreator.

Probably this is the answer.
All other IDEs and javac/java produce perfectly valid result ;)

s1 = Hello Java!!!
s1.toChar(7) = H
s1.toCharArray( ) = Hello Java!!!
s1.getChars(7, 11, a2, 0) = ava!
s1.substring(7, 11 ) = ava!

Alex.http://www.myjavaserver.com/~alexfromohio/- $Bp,i6Ho0zMQJ8;z(B -

- $Bp}<(Ho0zMQJ8;z(B -

If I move last 2 lines forward, then I can have right results.
Comment these three lines:
char a2[] = new char[5];
s1.getChars(7, 11, a2, 0);
System.out.println("s1.getChars(7, 11, a2, 0) = " + new String(a2));
will have also the results.

Why? Weird.
What kind of compiler or IDE do you use?

Mike
 
R

Roedy Green

String s2 = s1.substring(7, 11);
System.out.println("s1.substring(7, 11 ) = " + s2); // No results
here...
}

In debugging I often use code like this:
System.out.println("[" + s2 + "]");

It helps locate control chars, spaces, or other whitespace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top