Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
Read each individual word in string per 'if'

Is there a way to make Java read each individual word per 'if' in string? I will try my best to explain this...

public class test{
    public static void main(String[] args){
        String sentence = "testing1 testing2 testing3 testing4"

        //Scan first word \/ 
        if(sentence.equals("testing1"){
            //scan second word \/
            if(sentence.equals("testing2"){
                //scan third word \/
                if(sentence.equals("testing3"){
                    System.out.println("Works good!")
                }
            }
        }
    }
}   

If you can help me I would really appreciate it.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use Scanner.next() like below:

String sentence = "testing1 testing2 testing3 testing4";
try (Scanner sc = new Scanner(sentence)) {
    if (sc.next().equals("testing1")) {
        if (sc.next().equals("testing2")) {
            if (sc.next().equals("testing3")) {
                if (sc.next().equals("testing4")) {
                    System.out.println("Works good!");
                }
            }
        }
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

If I understand your question properly, you may try something like this:

public static void main(String[] args){
    String sentence = "testing1 testing2 testing3 testing4";
    String[] arrayOfWords = sentence.split(" "); // split the sentence according to the spaces
    Scanner in = new Scanner(System.in);

      //Scan first word
      System.out.println("Insert First Word");
      if(arrayOfWords[0].equals(in.next())){
            System.out.println("Insert Second Word");
            if(arrayOfWords[1].equals(in.next())){ 
               System.out.println("Insert Third Word");
               if(arrayOfWords[2].equals(in.next())){
                     System.out.println("Works good!");
               }
            }
        }
}

for example:

Insert First Word
testing1
Insert Second Word
testing2
Insert Third Word
testing3
Works good!
over 4 years ago · Santiago Trujillo Denunciar

0

String[] array = sentence.split(" ");

//Scan first word \/ 
if(array[0].equals("testing1"){
    //scan second word \/
    if(array[1].equals("testing2"){
        //scan third word \/
        if(array[2].equals("testing3"){
            System.out.println("Works good!")
        }
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda