YsXii
YsXii

Reputation: 19

Easy JAVA questions. But hard for me

When the first character of word starts with 'A' or 'a', let the program output the word 'America'. if the first character of the word starts with other characters, let the program prints "error"

public class Home1 {
    public static void main(String[] args) {
        String str=args[0];
        char ch;
        ch=   (1)   .    (2)    ;           
       if(      (3)      ) System.out.println("America");
          (4)    System.out.println("Error");
        }
}

I have figured out that 4th one is 'else' 3rd one may be something like, 'first character = 'a','A'

but i do not fully get them.

could you help me?

Upvotes: 0

Views: 59

Answers (2)

Stefan Freitag
Stefan Freitag

Reputation: 3608

(1) and (2): get somehow the char at position 0 of the string read. Documentation of the available methods on Strings is available here: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

(3) Compare the character read with 'A' and 'a':

If char equals 'A' or char equals 'a'.... Documentation can be found here: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html

Upvotes: 2

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

Ok this looks like a code fill in the blanks,

Your actual code should be something like this,

public static void main(String[] args) {
    String str = args[0];
    char ch;
    ch = str.charAt(0);
    if (ch == 'a' || ch == 'A')
        System.out.println("America");
    else
        System.out.println("Error");
}

So,

(1) = str
(2) = charAt(0)
(3) = ch == 'a' || ch == 'A'
(4) = else

Hope this helps.

Upvotes: 1

Related Questions