• Register
0 votes
23 views

I wrote a code in java to replace any charecter to a string:

public class NewClass1 {
    public static void main(String[] args) {
             char ch='D';       
        String str="laptop"; 
        ch = str + ch;      
        System.out.println(ch); 
    }
    
}

But it gives me error:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: java.lang.String cannot be converted to char
	at javastackoverflow.NewClass1.main(NewClass1.java:8)
C:\Users\Hp\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

How can I solve this problem?

10 7 2
6,050 points

1 Answer

0 votes
To add a charecter to the beginning, middle and end of a string in Java please insert, a char at the beginning and end of the string the simplest way is using string.You can try:
public class NewClass1 {
    public static void main(String[] args) {
             char ch='D';       
        String str="laptop"; 
        str = str + ch;      
        System.out.println(str); 
    }
    
}

You can also try:

public static void main(String[] args){
    String s1 = "laptop";
    s1 = s1.concat("D");
    System.out.println(s1);
  }
}
 

 

11 6 4
34,930 points

Related questions

0 votes
1 answer 6 views
6 views
I am finding difficulty with non repeated characters in a string in Java.
asked Sep 26 Daniel Anderson 4k points
0 votes
1 answer 7 views
7 views
I need to know how to remove the first cgaracter of a string in Java.
asked Sep 25 Daniel Anderson 4k points
0 votes
1 answer 43 views
43 views
I want to initialize a String in Java, but that string needs to include quotes; for example, I was thinking it is easy to put these into another double quote. But that is a wrong procedure because will be using two pair of double quotes and the message will lie ... , the compiler will think that my statement ends at second quote. System.out.print( hello world ); how to solve this problem?
asked Mar 16 LizzyM 6.1k points
0 votes
1 answer 5 views
5 views
I need to know how to print all palindrome permutations of a string in Java.
asked Oct 4 Daniel Anderson 4k points
0 votes
1 answer 10 views
10 views
Explain with an Example?
asked Sep 24 jagruthi 560 points