Question

How do I Write a java code where it records the user inputs of strings ....

How do I Write a java code where it records the user inputs of strings . <orderString>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;   // This will import just the Scanner class
import java.util.*;   // This will import the entire java.util package

Scanner s = new Scanner (System.in);

Here by writing Scanner s, we are declaring s as an object of Scanner class. System.in within the round brackets tells Java that this will be System Input i.e. input will be given to the system.

If you have understood all that has been stated till now, it's a good thing. If you haven't, do not bother. Currently, you just need to know that we have to import the class or package at the beginning of the program and then include the above line in the program.

Input Word

As we have seen in the above table, the method to input a word is next().

As we know that 10 is an integer, in the same way word is a string. A string is a collection of characters. Since word consists of characters, therefore it is a string.

String n;
n = s.next();      //   s is object of Scanner class

or simply

String n = s.next();      //   s is object of Scanner class

Here, String is a class whose object is n. You need not go into the details of classes and objects right now as these topics are discussed in detail later in this tutorial.

Let's see an example for this.

import java.util.*; class I3{  public static void main(String[] args){ Scanner s1 = new Scanner(System.in); System.out.println("Enter your first name"); String f = s1.next(); System.out.println("Enter your last name"); String l = s1.next(); System.out.println("Your name is " + f + l); } }

Enter your first name
Robert
Enter your last name
Brown
Your name is RobertBrown

Input a Sentence

In the same way as a word is a string, a sentence is also a string. Therefore, we input a sentence in the same way as that of word, the only difference is that the method to input it is nextLine().

String st = s.nextLine();      //   s is object of Scanner class

Consider an example.

import java.util.*; class I4{ public static void main(String[] args){ Scanner s1 = new Scanner(System.in); System.out.println("Enter your name"); String name = s1.nextLine(); System.out.println("Your name is " + name); } }

Enter your name
Robert Brown
Your name is Robert Brown

Add a comment
Know the answer?
Add Answer to:
How do I Write a java code where it records the user inputs of strings ....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT