Reputation: 444
I'm getting just spaces after running this code, it is not even printing "ABC"
..
import java.io.*;
class Str{
public static void main( String args[])
{
String a = "abc";
char ch[] = new char[2];
a.getChars(0,0,ch,1);
PrintWriter pw = new PrintWriter(System.out);
pw.println(ch);
pw.println("ABC");
pw.println(ch);
System.out.println(ch);
}
}
Upvotes: 0
Views: 46
Reputation: 120
getChars uses parameters (int srcBegin, int srcEnd, char[] dest, int destBegin). Your srcBegin and srcEnd are both 0. srcBegin needs to be 0 in your case, but srcEnd needs to be 3.
This works:
a.getChars(0,3,ch,0);
And you need a char array with the length 3 and not 2, so change char ch[]=new char[2]
to char ch[]=new char[3]
To copy only the first character into the ch array at index 1:
a.getChars(0,1,ch,1);
Upvotes: 2
Reputation: 8034
It seems you are lacking calling pw.flush()
, then something shows up. That should be the result of your program. You might have to change parameters in String.getChars()
method according to this Java tutorial, as you are receiving an empty array.
String a="abc";
char ch[]=new char[2];
a.getChars(1,2,ch,1); //Put indexes to first 2 positions to mark srcBegin, srcEnd
PrintWriter pw=new PrintWriter(System.out);
pw.println(ch);
pw.println("ABC");
pw.println(ch);
pw.flush();
Upvotes: 1