简体   繁体   中英

How to output a randomly chosen word and scramble the same word from a text file java

Below is the output that I have. I would like my scrambled word below to be the same as my random word. Right now its printing two different words from the text file and I want it to be the same, except one scrambled and the other regular. Below is my code.

在此处输入图片说明

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;


public class ScrambleWords {

    private static Scanner file;
    private static List<String> words = new ArrayList<String>();
    private static List<Character> characters = new ArrayList<>();

    public static void openFile() {

        try {
            file = new Scanner(new File("words.txt"));

        } catch (FileNotFoundException e) {
            System.out.println("File Not Found");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("IOException");
        }
    }

    public static String randomWord() {

        Random r = new Random();

        while(file.hasNext()) {
            words.add(file.next());
        }

        Collections.shuffle(words);
        String randomWord = words.get(r.nextInt(words.size()));

        return randomWord;
    }

    public static String readScramble(String randomWord) {

        for(char ch : randomWord.toCharArray()) {
            characters.add(ch);
        }

        Collections.shuffle(characters);
        StringBuilder sb = new StringBuilder();

        for(char ch: characters) { 
            sb.append(ch);
        }
        return sb.toString();
    }

    public static void main(String[] args) {

        openFile();

        String scramble = readScramble(randomWord());
        System.out.println("Scramble Word is: " + scramble);

        String random = randomWord();
        System.out.println("Random Word is: " + random);

    }

}

You're getting two different instances of random word. Modify your code to look like this:

 openFile();

    String word = randomWord();
    String scramble = readScramble(word);
    System.out.println("Scramble Word is: " + scramble);

    String random = word;
    System.out.println("Random Word is: " + random);

Change your main method:

  public static void main(String[] args) {

    openFile();

    String word = randomWord();

    String scramble = readScramble(word);
    System.out.println("Scramble Word is: " + scramble);

    System.out.println("Random Word is: " + word);

}

By the way, maybe you should post this code on the code review stack exchange.

You are calling randomWord() twice, thereby generating two random words. By definition of "random", you cannot expect the same word twice. Change you main method to

String w = randomWord();

then print w and its scramled version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM