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
Where can you access email accounts on your windows 7 computer in order to modify current accounts or create new ones?
Mademuasel [1]
I'll create new ones because just for me, its so hassle
8 0
3 years ago
Explain the main functions of the CPU.
bixtya [17]
The CPU performs basic arithmetic, logic, controlling, and input/output (I/O) operations specified by the instructions in the program. This contrasts with external components such as main memory and I/O circuitry, and specialized processors such as graphics processing units (GPUs).
4 0
3 years ago
Read 2 more answers
When making programmed decisions, the information needed to make this decision is __________, and the problem is highly structur
pentagon [3]

Answer:

readily available

Explanation:

The information needed to make this decision is readily available. That is because such decisions have been made countless times that strict guidelines have been created to handle any similar future decisions. These strict guidelines are organized as a sequence of events that must be followed accordingly and are always available for when the situation arises. Examples of this would be "What to do when a machine breaks?" or "What constitutes firing an employee?"

7 0
3 years ago
Help please fast!
serious [3.7K]

Answer:

the answer is: B: personal learning networks allow you to digitally collaborate with peers.

Explanation:

8 0
3 years ago
Does any one know how to code? If you do can you help me with my project?
fomenos

Answer:

kinda look it up!!!

Explanation:

go to code.org free coding

4 0
3 years ago
Read 2 more answers
Other questions:
  • To move one screen to the left press ____.
    9·1 answer
  • in cell h5, enter a formula that will calculate the percentage of attendees that went to the Altamonte springs job fair in 2018.
    15·2 answers
  • Mark for review (Will be highlighted on the review page) 11. A feature in Excel that allows you to graphically display in a sing
    10·1 answer
  • Write down the steps that you will take to organize your files
    12·2 answers
  • What is the first thing you should do when troubleshooting a computer problem
    5·2 answers
  • Which function of a word processing program enables you to apply a preset formatting
    5·1 answer
  • The engineering firm you work for is submitting a proposal to create an amphitheater at a state park. Proposals must be submitte
    15·1 answer
  • Given two int variables , firstplacewinner and secondplacewinner, write some code that swaps their values . declare any addition
    11·1 answer
  • I need help now with YT
    7·2 answers
  • A 4"x6" photo is digitized using 10,000 pixels. An 11"x7" photo is digitized using 30,000 pixels. Which image will have the bett
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!