Java Beginner: Cannot Find Character

I spent hours looking for a Java tutorial, trying to determine what I am doing wrong. The error I am returning is "I can not find the character" on line 13, which is the line with the code:

 System.out.println("The three initials are " + 
     getInitials(Harry, Joseph, Hacker));

Instructions are commented in code. I am sure this is due to the names I created. But I'm not sure.

public class InitialsTest {
     /**
       Gets the initials of this name
      @params first, middle, and last names
      @return a string consisting of the first character of the first, middle,
  and last name
      */

    public static void main(String[] args) {
         System.out.println("The three initials are " + 
         getInitials(Harry, Joseph, Hacker));
    }

    public static String getInitials(String one, String two, String three) {
        String initials = one.substring(0,1) + two.substring(0,1) + three.substring(0,1);
        return initials;
    }

 }
+5
source share
5 answers
System.out.println("The three initials are " 
    + getInitials("Harry", "Joseph", "Hacker")); //Enclosed within double quotes

This is how you pass literals String.

+16
source
System.out.println("The three initials are " + 
     getInitials("Harry", "Joseph", "Hacker"));

just use double quotes. if you declared them variables in your code, then double quotes are not needed,

+4
source

:

System.out.println("The three initials are " 
    + getInitials("Harry", "Joseph", "Hacker")); 

Harry, Joseph, Hacker ("") , , - .

. Java .

+3
source

You have 3 string values ​​passed in getInitials(), string literals must be enclosed in"

System.out.println("The three initials are " + 
          getInitials("Harry", "Joseph", "Hacker"));
+2
source

Lines should always be within the "and". So your code will be

System.out.println("The three initials are " + 
 getInitials("Harry", "Joseph", "Hacker"));

In addition, you can also use

String initials = one.charAt(0)+two.charAt(0)+three.charAt(0);

in your getInitials () function instead

String initials = one.substring(0,1) + two.substring(0,1) + three.substring(0,1);

Just saying it. Both give you a character at the 0th position of the index in String, but charAt returns as a character, not as a string.

0
source

All Articles