3

I'm trying to make a word scrambler for a project in class. It needs to read in a .txt file and output a String with all of the words in the file; but scrambled. for example:

This is how the words should be scrambled.

Ignoring punctuation and leaving the first and last letter in place.

I'm having trouble reading in the file into an array. I was planning on using collections.shuffle to shuffle the inner layers by ignoring the first and last letter and randomizing the inner letters.

I don't know how to implement the file reader, but here is my scrambler for each word.

public static String shuffle(String input){
    input = "supercalifragilisticexpialidocious";
    int i = 0;
    ArrayList<Character> chars = new ArrayList<Character>(input.length());
    String output = "";
    char[] characters = input.toCharArray();
    char[] newWord = new char[input.length()];



    newWord[0] = characters[0];
    newWord[input.length()-1] = characters[input.length()-1];

    for (i=1; i < input.length()-1;i++ ) {
        chars.add(characters[i]);
    }

    System.out.println(chars);
    for (i = 1; i <= input.length()-2; i++)
    {
        Collections.shuffle(chars);
    }
    Character[] middle = chars.toArray(new Character[chars.size()]);
    for (i = 1; i < newWord.length - 2; i++)
    {
        newWord[i] = middle[i];
    }

    System.out.println(newWord);
    StringBuffer r= new StringBuffer(output);
    for (i = 0; i < newWord.length-1; i++)
    {
        r.append(newWord[i]);
    }

    return output;
}

This outputs saxupieipclflaurrtocslcidigaiiois like I want, but now I need a way to read the .txt file in and split it into separate words.

If anyone can help me that would be great. What I'm really looking for is if someone can help me with making a file reader for this.

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
user2471416
  • 31
  • 1
  • 4

3 Answers3

1

use the filereader to read the file use the regex to split into words

    BufferedReader br = new BufferedReader( new FileReader( "file.txt" ) );
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while ( line != null ) {
            sb.append( line );
            sb.append( '\n' );
            line = br.readLine();
        }
        String everything = sb.toString();
        String[] words = everything.split( "[^\\w']+" );

        System.out.println( words );

    } finally {
        br.close();
    }
}
Nambi
  • 11,944
  • 3
  • 37
  • 49
  • So this will give me everything in the .txt file as a string of words separated by spaces? Do you recommend using .split to turn them into separate words? – user2471416 Sep 13 '13 at 01:14
1

Hope can help you. Thank for 1st and 2nd answers, got from them.

public class FileReader 
{

    public static void main(String[] args) {

        String filePath = "data.txt";
        File file = new File(filePath);
        Scanner scanner = null;
        List<String> words = new ArrayList<String>();
        try {
            scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                words.addAll(getWords(line));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            scanner.close();
        }
        for(String word : words)
        {
            System.out.println(word);
        }
    }

    public static List<String> getWords(String text) {
        String[] words = text.split("[^\\w']+");
        return Arrays.asList(words);
    }
}
Community
  • 1
  • 1
Wille Lee
  • 11
  • 2
0

For brevity I recommend to use Scanner class:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerReadFile {

    public static void main( String[] args ) {

        final File file = new File( "data.txt" );

        try ( final Scanner scanner = new Scanner( file ); ) {
            while ( scanner.hasNextLine() ) {
                String line = scanner.nextLine();
                System.out.println( line );
            }
        } catch ( FileNotFoundException e ) {
            e.printStackTrace();
        }
    }
}
Igor Rodriguez
  • 1,196
  • 11
  • 16