1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Dmitrij [34]
3 years ago
11

Imagine the user types in a complete sentence, but we want to read only the first word and place it in the String variable word.

What are the statements that will declare the Scanner object and read the value of word
Computers and Technology
1 answer:
Vladimir [108]3 years ago
8 0

Answer:

import java.util.Scanner;

public class FirstWord {

   public static void main(String[] args) {

       //NOW YOU WANT TO COLLECT INPUTS FROM THE STUDENT OR USER

       //IMPORT THE SCANNER CLASS FIRST  outside your class

       // THEN YOU CREATE AN OBJECT OF THE SCANNER CLASS

       Scanner input = new Scanner(System.in);

       

       //CREATE A VARIABLE TO HOLD THE USER INPUT

       String text = input.nextLine();

       

       //The variable holder will be used to store characters from the for loop

       String holder="";

       

       //Create a for loop that loops through the user's input

       for(int i=0; i<text.length(); i++){

           //As the loop increases, we get the character in the user's input and store them one by one in the variable holder

           holder+=text.charAt(i);

           if(text.charAt(i)==' '){

               //If the loop encounters a space, it means a complete word has been gotten from the user's input

               //Then the break below Breaks the loop, and the loop stops running

               break;

           }

       }

       System.out.println(holder);

}

Explanation:

text.length() returns the length of the sequence of characters present in length

text.charAt(a number) Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

Create a for loop

How does a for loop work?

It collects a starting point, an end point and an increment respectively....all these are passed in as arguments into the for loop

For example, the code above starts from 0, calculates the number of characters in the user's input and sets it as the end point, the loop runs from zero to the length of the text of the user...

Note: i++ in the loop is the increment, it means the loop increases by 1

You might be interested in
The set of instructions that tell the computer what to do are called ____.
CaHeK987 [17]
Hi,

The word you are looking for is "programs".

Hope this helps.
r3t40
4 0
3 years ago
Read 2 more answers
What Intel socket recommends the use of a liquid cooling system?
Komok [63]
The socket which Intel recommends that one should use with a liquid cooling system is LGA 2011. LGA 2011, also known as socket R is a CPU socket manufactured by Intel. It was released into the market in November 2011 and it replaced LGA 1366 and LGA 1567 in the performance and high end desk tops and server platforms. Socket R has 2011 pins that touch contact points on the underside of the processor.
7 0
3 years ago
Write the steps for renaming an existing folder​
vekshin1

Answer:

Rename a file or folder

Explanation:

1.Right-click on the item and select Rename, or select the file and press F2 .

2.Type the new name and press Enter or click Rename.

8 0
3 years ago
Read 2 more answers
How do you return a value from a function?
rjkz [21]

Answer:

return instruction used to return a value from a function.

Explanation:

Function is a block of statement which perform the special task.

Syntax for define a function:

type name(parameter_1, parameter_2,...)

{

  statement;

return variable;

}

In the syntax, type define the return type of the function. It can be int, float, double and also array as well. Function can return the array as well.

return is the instruction which is used to return the value or can use as a termination of function.

For return the value, we can use variable name which store the value or use direct value.

4 0
3 years ago
Why is it important to follow a consistent naming convention for variables?
Vitek1552 [10]

Answer:

today I am going to be out for the job interview so if I will make it happen I am going to New Orleans this week to get the kids to work on the spot and I can get ready to go out to dinner and I can make a few more people

4 0
2 years ago
Other questions:
  • Which reading strategy refers to reading only the key words and phrases?
    13·2 answers
  • What does the rule of five say?
    12·2 answers
  • Define the missing method. licenseNum is created as: (100000 * customID) licenseYear, where customID is a method parameter. Samp
    14·2 answers
  • Who was the founder of the location-sharing site Whrrl
    6·1 answer
  • How does the post process alert the user if it detects a hardware problem during the post process?
    6·1 answer
  • Rewritable (write, erase, write again) is known as _______.
    7·1 answer
  • Which model represents any process in general?
    6·1 answer
  • Using C++
    13·1 answer
  • Describe a NIC card and can you have more than one?
    13·1 answer
  • Disadvantages of the divisional organization structure are _____. each division is able to operate independently from the parent
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!