0

I want to enter string value and writing first char to screen.

Code:

    Scanner giris = new Scanner(System.in);
    
    System.out.print("Enter Name: ");
    String ad = giris.next();
    System.out.print("Enter Second Name: ");
    String ikinciad = giris.next();
    System.out.print("Enter Lastname: ");
    String soyad = giris.next();
    
    char one = ad.charAt(0); //ilk karakterini almak için 0 yazdik
    char two = ikinciad.charAt(0); //ikinci olsaydi 1 yazardik 
    char three = soyad.charAt(0);
    
    System.out.print("Initials: ");
    System.out.print(one+two+three);
    
    giris.close();

Output:

     Enter Name: cem
     Enter Second Name: ahmet
     Enter Lastname: mehmet
     Initials: 305
Jens
  • 67,715
  • 15
  • 98
  • 113
Cem
  • 1
  • 2
    `char` is a (specialized) unsigned number. If you want that to work, you need to 'signal' that a string is required: `System.out.print(""+one+two+three);` – g00se Apr 16 '23 at 10:56
  • 1
    `+` only works as string concatenation if at least one operand is a `String` - not the case in posted code - Java Language Specification: [15.18. Additive Operators](https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.18): "*If the type of either operand of a `+` operator is `String`, then the operation is string concatenation.*" – user16320675 Apr 16 '23 at 11:16

0 Answers0