0

Im a novice java learner, i have a little problem and i hope you guys can help me out. i have a Names.txt file that contains a huge amount of random names, each line has an appropriate name (expl: jhon Micheal Anthony etc...) I've been writing a function that randomly choses one of these names:

public static void RandomNameGenerator() throws FileNotFoundException
{
    // the File.txt has organized names, meaning that each line contains a name
    //the idea here is to get a random int take that number and find a name corresponding to that line number
    int txtnumlines = 0; // how many lines the txt file has
    Random random = new Random();
    Scanner file = new Scanner(new File("Names.txt")); //loads the txt file
    while (file.hasNext()) //counts the number of lines
    {
        file.nextLine();
        txtnumlines += 1;
    }
    int randomintname = random.nextInt(txtnumlines);
    // takes a random number, that number will be used to get the name from the txt line
    String RandomName = "";

    // I'm stuck here :(
}

the problem is i don't know how to continue, more specifically how to extract that name (let's say alex) using the random integers i have that represents a random line

hope my question was clear, Thank you for helping me out!

Naito
  • 63
  • 7
  • Does this answer your question? [How to read a specific line using the specific line number from a file in Java?](https://stackoverflow.com/questions/2312756/how-to-read-a-specific-line-using-the-specific-line-number-from-a-file-in-java) – Amongalen Dec 16 '19 at 14:29
  • Specificaly the answer with the most points, not the accepted one – Amongalen Dec 16 '19 at 14:30
  • Thank you sir amongalen, it worked now :) – Naito Dec 16 '19 at 14:43

2 Answers2

0

Here, you might want to try this:

public static void RandomNameGenerator() throws FileNotFoundException
{
    // the File.txt has organized names, meaning that each line contains a name
    //the idea here is to get a random int take that number and find a name corresponding to that line number
    int txtnumlines = 0; // how many lines the txt file has
    Random random = new Random();
    Scanner file = new Scanner(new File("Names.txt")); //loads the txt file
    Scanner fileReloaded = new Scanner(new File("Names.txt")); //Let's use another one since we can't reset the first scanner once the lines have been counted (I'm not sure tho)
    while (file.hasNext()) //counts the number of lines
    {
        file.nextLine();
        txtnumlines += 1;
    }
    int randomintname = random.nextInt(txtnumlines);
    // takes a random number, that number will be used to get the name from the txt line

    int i = 0;
    String RandomName = "";
    while(fileReloaded.hasNext())
    {
        String aLine = fileReloaded.nextLine();
        if (i == randomintname) Randomname = aLine;
        i++;
    }

    // Now you can do whatever you want with the Randomname variable. You might want to lowercase the first letter, however. :p
}
Kayn Serhal
  • 123
  • 7
  • Thanks sir, i tired similair approach like this but it was in vain due to lack of experience – Naito Dec 16 '19 at 14:52
0

Reading all lines

String[] lines = new Scanner(
    MyClass.class.getClassLoader().getResourceAsStream("Names.txt"),
    StandardCharsets.UTF_8.name()
).next().split("\\A");

Extract random line

Random random = new Random();
String randomLine = lines[random.nextInt(lines.length)].trim();
Paul
  • 1,410
  • 1
  • 14
  • 30