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

181
Vistas
Can you use the value of a variable to select an already defined variable with that same name?

I'm trying to try something out for my AP statistics class where I need to randomly select five words from the lyrics of a song and calculate the average length of those strings. This is what I have so far: (There's 297 lyrics but I don't want to type all of those out if it won't work)

        String n001 = "I";
        String n002 = "look";
        String n003 = "and";
        String n004 = "stare";
        String n005 = "so";
        
        Random bob = new Random();
     
        String num_1 = String.format("%03d", bob.nextInt(298)); 
        System.out.println(num_1);
        String num_2 = String.format("%03d", bob.nextInt(298)); 
        System.out.println(num_2);
        String num_3 = String.format("%03d", bob.nextInt(298)); 
        System.out.println(num_3);
        String num_4 = String.format("%03d", bob.nextInt(298)); 
        System.out.println(num_4);
        String num_5 = String.format("%03d", bob.nextInt(298)); 
        System.out.println(num_5);
        
        String num1 = "n" + num_1; 

What I can't figure out is how to take the value in num1 to select one of the Strings named the same thing. I need to do that for all five selected random numbers.

I'm sure there's a way to do this, but it's my first year in a CS class and I can't seem to find the answer anywhere.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Here is a suggestion.

  • put all the words in a List<String>
    (e.g. List<String> = new ArrayList<>())
  • Use Collections.shuffle to shuffle the list.
  • then take the first 5 entries.

Doing the above will guarantee you won't repeat any word unless there are duplicates in the list.

Figuring out how to average the lengths is up to you.

about 4 years ago · Juan Pablo Isaza Denunciar

0


package RandomLyric;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RandomLyric 
{

    public static void main(String[] args) 
    {
        List<String>bob = new ArrayList<>();
        
        bob.add("I");
        bob.add("look");
        bob.add("and");
        bob.add("stare");
        bob.add("so");
        bob.add("deep");
        bob.add("in");
        bob.add("your");
        bob.add("eyes");
        bob.add("I");
        bob.add("touch");
        bob.add("on");
        bob.add("you");
        bob.add("more");
        bob.add("and");
        bob.add("more");
        bob.add("every");
        bob.add("time");
        bob.add("when");
        bob.add("you");
        bob.add("leave");
        bob.add("I'm");
        bob.add("begging");
        bob.add("you");
        bob.add("not");
        bob.add("to");
        bob.add("go");
        bob.add("call");
        bob.add("your");
        bob.add("two");
        bob.add("or");
        bob.add("three");
        bob.add("times");
        bob.add("in");
        bob.add("a");
        bob.add("row");
        bob.add("Such");
        bob.add("a");
        bob.add("funny");
        bob.add("thing");
        bob.add("for");
        bob.add("me");
        bob.add("to");
        bob.add("try");
        bob.add("to");
        bob.add("explain");
        bob.add("how");
        bob.add("I'm");
        bob.add("feeling");
        
        
        Collections.shuffle(bob);
        java.util.Iterator<String> itr=bob.iterator();
        String num1_ = itr.next();
        double num1 = num1_.length();
        System.out.printf( num1_ + ": %.0f",+ num1);
        System.out.println("");
        
        String num2_ = itr.next();
        double num2 = num2_.length();
        System.out.printf( num2_ + ": %.0f",+ num2);
        System.out.println("");
        
        String num3_ = itr.next();
        double num3 = num1_.length();
        System.out.printf( num3_ + ": %.0f",+ num3);
        System.out.println("");
        
        String num4_ = itr.next();
        double num4 = num4_.length();
        System.out.printf( num4_ + ": %.0f",+ num4);
        System.out.println("");
        
        String num5_ = itr.next();
        double num5 = num5_.length();
        System.out.printf( num5_ + ": %.0f",+ num5);
        System.out.println("");
        
        double avg = (num1 + num2 + num3 + num4 + num5)/5;
        System.out.println("The length of the five randomly selected strings is: " + avg);
        
        
        
        
        
        
    }

}

Thank you!!!! I got it all together now!

about 4 years ago · Juan Pablo Isaza 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