d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help With A Program
Add Reply New Topic New Poll
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Oct 1 2013 09:04am
How do I read in a .txt file that has a bunch of words that are like 4-5 letters.

Then take those words and randomly pick one.

After a random one has been chosen, randomly jumble all the letters in the words.

Display all of the random word's jumbled letters.

Then ask for input from the user to guess what the word is and compare the two strings to see whether or not they are equal.
Member
Posts: 16,411
Joined: Apr 9 2007
Gold: 12,059.17
Oct 1 2013 09:42am
You did not write which language :)


here you have an example how to read a file line by line in java:
http://answers.yahoo.com/question/index?qid=20091102215123AAmOPmG


after you read all the words into the collection (Arraylist add) you can use Collection's shuffle method to shuffle the arraylist and then just take the first element of this shuffled arraylist: arraylist.get(0)


Also here is an algorithm to shuffle word in java:

ArrayList<Character> chars = new ArrayList<Character>(word.length());
for ( char c : word.toCharArray() ) {
chars.add(c);
}
Collections.shuffle(chars);
char[] shuffled = new char[chars.size()];
for ( int i = 0; i < shuffled.length; i++ ) {
shuffled[i] = chars.get(i);
}
String shuffledWord = new String(shuffled);

here you have an example how to read line from console (console = user input)

http://alvinalexander.com/java/edu/pj/pj010005

good luck :-)

This post was edited by Ironfister on Oct 1 2013 09:46am
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Oct 1 2013 09:44am
this is java, forgot to mention that
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 1 2013 09:46am
Code
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Main {

public static void main( String[] args ) throws IOException {

// Create a BufferedReader
BufferedReader bufferedReader = null;

// Create a list to store the words from the file
List<String> words = new ArrayList<String>( );

String currentLine;

// initialize the bufferedReader and pass a new FileReader with the location
// of your file as the parameter
bufferedReader = new BufferedReader( new FileReader( "C:\\test\\words.txt" ) );

while ( (currentLine = bufferedReader.readLine( )) != null ) {
// Loop through each line in the file and add it to the words List
words.add( currentLine );
}

// Get a random word from the words List by using a random index
String randomWord = words.get( new Random( ).nextInt( words.size( ) + 1 ) );

// Convert the random word into individual strings in a list
List<String> letters = Arrays.asList( randomWord.split( "" ) );

// Use the standard Collections.shuffle function to rearange the letters
Collections.shuffle( letters );

// Create a new string to store the new letters
String jumbledWord = "";

// loop through each letter in the letters list and add them to the new
// string
for ( String letter : letters ) {
jumbledWord += letter;
}

System.out.println( "Selected word: " + randomWord + "\nShuffeled word: "
+ jumbledWord );
}
}
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Oct 1 2013 11:17am
Quote (labatymo @ Oct 1 2013 10:46am)
Code
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Main {

  public static void main( String[] args ) throws IOException {

    // Create a BufferedReader
    BufferedReader bufferedReader = null;

    // Create a list to store the words from the file
    List<String> words = new ArrayList<String>( );

    String currentLine;

    // initialize the bufferedReader and pass a new FileReader with the location
    // of your file as the parameter
    bufferedReader = new BufferedReader( new FileReader( "C:\\test\\words.txt" ) );

    while ( (currentLine = bufferedReader.readLine( )) != null ) {
      // Loop through each line in the file and add it to the words List
      words.add( currentLine );
    }

    // Get a random word from the words List by using a random index
    String randomWord = words.get( new Random( ).nextInt( words.size( ) + 1 ) );

    // Convert the random word into individual strings in a list
    List<String> letters = Arrays.asList( randomWord.split( "" ) );

    // Use the standard Collections.shuffle function to rearange the letters
    Collections.shuffle( letters );

    // Create a new string to store the new letters
    String jumbledWord = "";

    // loop through each letter in the letters list and add them to the new
    // string
    for ( String letter : letters ) {
      jumbledWord += letter;
    }

    System.out.println( "Selected word: " + randomWord + "\nShuffeled word: "
        + jumbledWord );
  }
}


I haven't learned bufferedReader or array.list

I need to read the words into an array not an array list.
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Oct 1 2013 11:20am
Use a scanner with the text file as a parameter

File file = new File("nameoffile.txt");

Scanner sc = new Scanner(file);

while (sc.hasNext())
String next = sc.next();

This post was edited by oOn on Oct 1 2013 11:22am
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Oct 1 2013 11:22am
Quote (oOn @ Oct 1 2013 12:20pm)
Use a scanner with the text file as a parameter


Scanner scanner = new Scanner(words.txt)

like that?
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Oct 1 2013 11:22am
Updated my post
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 1 2013 11:40am
Quote (GetSpanked @ Oct 1 2013 01:22pm)
Scanner scanner = new Scanner(words.txt)

like that?


Scanner takes a File as it's parameter

So it would be
Code
Scanner scanner = new Scanner(new File("words.txt"));


or

Code
File file = new File("words.txt");

Scanner scanner = new Scanner(file);
Member
Posts: 16,411
Joined: Apr 9 2007
Gold: 12,059.17
Oct 1 2013 12:08pm
ArrayList would be better here because array has a fixed size.

So with array either put a hardcoded limit (like 100 words max) or create bigger table every like 10 records and copy old contents.

simpler solution with fixed size like 100 elements (can be more ofcourse if u put bigger number)

File file = new File("nameoffile.txt");

Scanner sc = new Scanner(file);

String[] words = new String[100];
int wordCounter = 0;

while (sc.hasNext()){
String next = sc.next();
words[wordCounter] = next;
wordCounter++;
}

edit: let me know if you get it or not so we can go forward or need to explain more.

This post was edited by Ironfister on Oct 1 2013 12:12pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll