Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class trim_con_substring{
- public static void main (String args[]) {
- String frase = validaFrase();
- quitaEspacios(frase);
- }
- public static String validaFrase(){
- Scanner tec = new Scanner(System.in);
- String frase;
- boolean noSirve = true;
- do{
- System.out.print("Ingrese una frase valida:");
- frase = tec.nextLine();
- if(frase.length()==0){ //Verifica que no sea nula.
- noSirve=true;
- System.out.println("Error! La frase ingresada es nula.");
- }else{ //Si no es nula, verifica los espacios.
- for(int i=0;i<frase.length();i++){
- if(frase.charAt(i)!=' '){
- noSirve=false;}
- }if(noSirve){
- System.out.println("Error! Frase compuesta de espacios.");}}
- }while(noSirve);
- return frase;
- }
- public static void quitaEspacios(String frase){
- int x=0;
- int y=0;
- for(int i=0;i<frase.length();i++){
- if (frase.charAt(i)!=' '){
- y=i;}} //Obtiene y = posicion de la ultima letra.
- for(int i=frase.length()-1;i>=0;i--){
- if (frase.charAt(i)!=' '){
- x=i;}} //Obtiene x = posicion de la primera letra.
- System.out.println("*"+frase.substring(x,y+1)+"*");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement