Read the Java string from the console and reverse to remove spaces

//A code block

import java.util.Scanner;

public class QuestionOneb {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str= sc.nextLine();
	    String out = "";
	    	for(int i=str.length()-1; i>=0; i--){//方法一
	    		if(str.charAt(i)!=' ')
	    		out = out+str.charAt(i);
	    	}
	    	System.out.println(out);
	    }
}

The operation of inputting and reading a string from the keyboard is used.
Scanner class
nextLine() method (return String class)
uses two string functions:
1. Get the length, length()
2. Get the character at the label charAt(int index )
You can also use the method in the StringBuffer class to
return the string StringBuffer(String s).reverse()

Guess you like

Origin blog.csdn.net/GreatSimulation/article/details/106748051