-2

So I'm doing a program that takes a user input and when it finds chars that are similar to numbers, it replaces it by the number. (For example, it replaces O's by 0's, e's by 3's, etc) The problem is that when it finds a blank space it all messes up. You can check by compiling the code that the output is completely messed up.

/* Program to encrypt text replacing some letters by similar numbers
Done by: Gabriel Mello
*/
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String input; //Allocating space for user input
    char[] output=new char[100000]; //Allocating space for final output
    while(true){ // Lets it work as many times as wished


     System.out.println("Escribí la frase que quieras transformar"); // Spanish for input your frase
     input=sc.next(); //Takes user input

         for(int i=0; i<=input.length()-1;i++){ //Iterates over every char in the input
                     switch(input.charAt(i)){//Checks wether the current digit is valid for replacement,
                     case 'O':                    // if it is, it replaces it, if not, it leaves it as it is.
                     case 'o': output[i]='0';
                     break;
                     case 'L':
                     case 'l': 
                     case 'I':
                     case 'i': output[i]='1';
                     break;
                     case 'Z':
                     case 'z': output[i]='2';
                     break;
                     case 'E':
                     case 'e': output[i]='3';
                     break;
                     case 'A':
                     case 'a': output[i]='4';
                     break;
                     case 'S':
                     case 's': output[i]='5';
                     break;
                     case 'G':
                     case 'g': output[i]='6';
                     break;
                     case 'T':
                     case 't': output[i]='7';
                     break;
                     case 'B':
                     case 'b': output[i]='8';
                     break;
                     case 'P':
                     case 'p': output[i]='9';
                     break;
                     default: output[i]=input.charAt(i);
                     }

         }

             System.out.println(output); //Prints the output
             for(int i=0;i<=output.length-1;i++){ //Resets the output array
                 output[i]=' ';
             }
    }


}
}
Otomeram
  • 13
  • 1
  • 5

1 Answers1

1

The Scanner input splits at spaces. So if you type in 12 34 the first input your code sees is 12. It Runs through the for(int i=0; i<=input.length()-1;i++) loop, then the while loop finds another input 34 and again runs through it. See the Java doc:

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

Replace input=sc.next() by input=sc.nextLine() to fix it.

dumbPotato21
  • 5,353
  • 5
  • 19
  • 31
Martin G
  • 219
  • 1
  • 6