Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Hint: On how you can find how to work with the first character of a string

Hint: On how you can find how to work with the first character of a string

Computer Science

Hint: On how you can find how to work with the first character of a string.

Reading a Character

Sometimes you will want to read a single character from the keyboard. For example, your program might ask the user a yes/no question, and specify that he or she type Y for yes or N for no, The scanner class does not have a method for reading a single character, however. The approach that we will use in this book for reading a character is to use the scenner class's nextLine method to read a string from the keyboard, then use the string class's charat method to extract the first character of the string. This will be the character the user entered at the keyboard. Here is an example:

String input; // To hold a line of input

char anover; // To hold a single cheracter

#1 Create @ Scanner object tor keyboard input.

Soanmer keyboard © new Scanner(System, in};

¢/ bak the cser a qoestion.

System. out .print("Are you having fun? (Teyes, enc) “)3

inpot « keyboard.nextLine(); // Get @ line of inget.

ansver © ingut.charat(0)) /é Gat the first charecter.

 

The input variable references a string object. The last statement in this code calls the string class's charat method to retrieve the character at position 0, which is the first character in the string. After this statement executes, the answer variable will hold the character that the user typed at the keyboard.

Mixing Calls to nextLine with Calls to Other scanner Methods

When you call one of the scanner class's methods to read a primitive value, such as nextiInt of nextDouble, then call the nextLine method to read a string, an annoying and hard-to-find problem can occur. For example, look at the program in Code Listing 2-30.

 

Code Listing 2-30 (1nputProblem. java)

1 import java.util.Scanoar; // Needed for the Scanner class

2

3 fee

4 = ¢ This program has eo problem reading input.

 

s tf

 

 

? public class InputProbles

 

at

 

3 public static void sain(String!) arge)

 

1

 

11s String name; // To hold the user‘s name

120 int ages = // To hold the user's age

 

1a double income; // To hold the user's income

4

 

15s ¢# Create @ Scanner object to read input.

16 Seanner keyboard © new Gcannex(System.in};

7

 

is ?/ Get the user's age.

 

19 System.out.print(“What is your age? “)?

26 age = keyboard.nextInt({);

 

21

 

22 #7 Get the user's incoms

 

33 Bystem.cut.print(“What ie your annual income? “);

a income = keyboard. nextDoublet);

 

2s

 

26 // Get the user's name.

 

2? System.out.print(“What is your name? “);

28°) nama © keyboard. nextLioa(}7

 

 

 

 

 

 

QUESTION

Modify the code listing 2-32 (NamesDialog.java) from page 97 such that it will display only the first name and last name initials. For example, if the user enters John as first name, and Smith as last name, then the program should display "Hello JS" inside the message box.

 

For the name… My First Name is Ibiyinka

                           My Last name is Ibitoye

You can use my name instead of using a random name or instead of using someone elses name

 

 

QUESTION

 

 

 

 

 

 

 

 

 

 

 

Option 1

Low Cost Option
Download this past answer in few clicks

18.99 USD

PURCHASE SOLUTION

Already member?


Option 2

Custom new solution created by our subject matter experts

GET A QUOTE