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
jekas [21]
2 years ago
13

How do I find a space in my String and replace it? JAVA

Computers and Technology
1 answer:
Fantom [35]2 years ago
8 0

Answer:

This article shows how to use regex to remove spaces in between a String.

A string with spaces in between.

String text = "Hello World Java.";

We want to remove the spaces and display it as below:

Hello World Java.

1. Java regex remove spaces

In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.

Regex explanation.

`\\s` # Matches whitespace characters.

+ # One or more

StringRemoveSpaces.java

package com.mkyong.regex.string;

public class StringRemoveSpaces {

public static void main(String[] args) {

String text = "Hello World Java.";

String result = text.replaceAll("\\s+", " ");

System.out.println(result);

}

}

Output

Terminal

Hello World Java.

You might be interested in
The prediction that the number of transistors on a chip would double about every two years is known as ________. Metcalfe's law
MaRussiya [10]

Answer:

Moore's law

Explanation:

The Moore’s law which was named after the pioneer, Gordon Moore, predicted that the number of transistors on a chip would double about every two years. This law is one of the reasons why computers became so powerful. These transistors and chips are used to make mathematical calculations and in 1965, Gordon made an observation and forecasted that the number of transistors that can be placed in any ICs doubles approximately every two years. Moore was so convinced about this prediction that he went on to co-found the biggest chips processor; INTEL. This trend has been accurate since then but has started to slow down from 2013.

5 0
2 years ago
Differentiated instruction is designed to educate children through ________ techniques in order to ensure every student is being
EleoNora [17]
They use multiple techniques to help children. 
7 0
2 years ago
Description:
Nastasia [14]

Answer:

The programming language is not stated (I'll answer using C++)

#include <iostream>

using namespace std;

int convert(float miles)

{

   return miles * 5280;

}

int main() {

   cout<<"Console:"<<endl;

   cout<<"Hike Calculator"<<endl;

   float miles;

   char response;

   cout<<"How many miles did you walk?. ";

   cin>>miles;

   cout<<"You walked "<<convert(miles)<<" feet"<<endl;

   cout<<"Continue? (y/n): ";

   cin>>response;

   while(response == 'y')

   {

   cout<<"How many miles did you walk?. ";

   cin>>miles;

   cout<<"You walked "<<convert(miles)<<" feet"<<endl;

   cout<<"Continue? (y/n): ";

   cin>>response;

   }

   cout<<"Bye!";

   return 0;

}

Explanation:

Here, I'll explain some difficult lines (one after the other)

The italicized represents the function that returns the number of feet

<em>int convert(float miles) </em>

<em>{ </em>

<em>    return miles * 5280; </em>

<em>} </em>

The main method starts here

int main() {

The next two lines gives an info about the program

   cout<<"Console:"<<endl;

   cout<<"Hike Calculator"<<endl;

   float miles;

   char response;

This line prompts user for number of miles

   cout<<"How many miles did you walk?. ";

   cin>>miles;

This line calls the function that converts miles to feet and prints the feet equivalent of miles

   cout<<"You walked "<<convert(miles)<<" feet"<<endl;

This line prompts user for another conversion

   cout<<"Continue? (y/n): ";

   cin>>response;

This is an iteration that repeats its execution as long as user continue input y as response

<em>    while(response == 'y') </em>

<em>    { </em>

<em>    cout<<"How many miles did you walk?. "; </em>

<em>    cin>>miles; </em>

<em>    cout<<"You walked "<<convert(miles)<<" feet"<<endl; </em>

<em>    cout<<"Continue? (y/n): "; </em>

<em>    cin>>response; </em>

<em>    } </em>

   cout<<"Bye!";

8 0
3 years ago
There is an interface I that has abstract class AC as its subclass. Class C inherits AC. We know that C delegates to an object o
Aloiza [94]

Answer:

are u in HS or college work

I am trying to understand

6 0
2 years ago
(1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output
Alex787 [66]

Answer:

import java.util.*;

public class Main{

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

 int intgVal; double dblVal; char chrVal; String strVal;

 System.out.print("Enter integer: ");  

 intgVal = input.nextInt();

 System.out.print("Enter double: ");  

 dblVal = input.nextDouble();

 System.out.print("Enter character: ");  

 chrVal = input.next().charAt(0);

 input.nextLine();

 System.out.print("Enter string: ");  

 strVal = input.nextLine();

 System.out.println(intgVal+" "+dblVal+" "+chrVal+" "+strVal);

 System.out.println(strVal+" "+chrVal+" "+dblVal+" "+intgVal);

 int IntValue = (int) dblVal;

 System.out.print("Cast to an integer: "+IntValue);

}

}

Explanation:

See attachment for complete program where comments were used to explain each line of the program

Download txt
6 0
3 years ago
Other questions:
  • If you wanted to round $3.99 located in Cell B3 to the nearest dollar, what is the correct Microsoft excel formula?
    6·2 answers
  • ___________is a security strategy that applies multiple layers of defense because there is an assumption that any single protect
    12·1 answer
  • ________ gives its approval to U.S. e-commerce websites that follow strict privacy standards, such as explaining to visitors how
    6·1 answer
  • Together with some anthropologists, you’re studying a sparsely populated region of a rainforest, where 50 farmers live along a 5
    15·1 answer
  • Which of the following statements is true? Computer disks are volatile storage devices Volatile storage is lost when a computer
    12·2 answers
  • Which of the following Web sites would be MOST credible?
    6·1 answer
  • Being nice take the points​
    9·1 answer
  • What does an "embedded video" mean
    7·1 answer
  • Consider the following code segment
    15·1 answer
  • What is the difference between information poor and information rich<br>​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!