Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Trying to get one line of my string to print and then scramble the code. Cant figure it out.

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to get one line of my string to print and then scramble the code. Cant figure it out.

    Hey guys I cant Figure out how to choose the first line of my random string of words and jumble the characters. I have been at it for hours and cant figure it out.
    Help would be appreciated.



    import java.util.*;
    import java.io.*;


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

    Scanner inFile = new Scanner(new File("words.txt"));
    Random r = new Random();
    List<String> words = new ArrayList<String>();

    while (inFile.hasNext()) {
    words.add(inFile.nextLine());
    String[] array = words.toArray(new String [0]); //String word = inFile.nextLine();
    String test1 = (array[new Random().nextInt(array.length)]);
    String[] lines = test1.split("\n");
    String firstword = test1.substring(0);


    boolean [] used = new boolean [test1.length()];
    String jumble = "";
    int count = 0;
    while (count < test1.length()) {
    int index = r.nextInt(test1.length());


    jumble = jumble + test1.charAt(index);

    count++;


    }
    System.out.println(test1);


    }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trying to get one line of my string to print and then scramble the code. Cant figure it out.

    Welcome to the forum.

    Please use code tags when posting code. See the Announcements page if you need hlep.

    What does the program do so far? Does it compile and run? Where are you stuck?

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get one line of my string to print and then scramble the code. Cant figure it out.

    Hey thanks for your reply and I will make sure to do that next time.
    Right now my program does compile. When run it imports a .txt file of dictionary words, then prints out a random list of the words. What I want is for the program to print out only one random word instead of all of them.

    --- Update ---

    Anyone have any ideas?

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trying to get one line of my string to print and then scramble the code. Cant figure it out.

    If you want something to happen only once, then inside the body of a loop may not be the best place for that statement.

    Move your print statement out of the loop. Don't forget to decide which word to print, since the current method prints them all, you will need some way to choose which word will be the only one printed.

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get one line of my string to print and then scramble the code. Cant figure it out.

    Quote Originally Posted by jps View Post
    If you want something to happen only once, then inside the body of a loop may not be the best place for that statement.

    Move your print statement out of the loop. Don't forget to decide which word to print, since the current method prints them all, you will need some way to choose which word will be the only one printed.

    I cant figure out how to only print one word from the list. I have tried string.split and tokenizer but neither of them seem to work. Some more help would be greatly appreciated.

  6. #6
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Jumbler word game is not jumbling random words correctly. Always repeats 2 letters.

    This program picks a random word from my words.txt file and then prints the word and under it prints the word jumbled. The jumbled word always repeats one character. For example ("door" would scramble to be "rord"). I am not sure how to fi this problem.



    import java.util.*;
    import java.io.*;
     
     
    public class new29{
    public static void main (String [] args) throws IOException{
     
    Scanner inFile = new Scanner(new File("words.txt"));
    ArrayList<String> words = new ArrayList<String>();
     
    Random r = new Random();
    while (inFile.hasNext())	{
    words.add(inFile.next());
    }
     String[] array = words.toArray(new String [0]);                             //String word = inFile.nextLine();
    String test1 = (array[new Random().nextInt(array.length)]);
     
    System.out.println(test1);
     
     
     
    String jumble = " ";
    int count = 0;
    while (count < test1.length())	{
    boolean [] used = new boolean [test1.length()];
    int index = r.nextInt(test1.length());
     
    jumble += test1.charAt(index);
     
    count++;
    }
    System.out.println("word to unscramble: " +jumble);
     
     
    	}
    }

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trying to get one line of my string to print and then scramble the code. Cant figure it out.

    Your strings are stored in an array.
    Arrays are accessed through the index number of their position in the array. Think of it like a train, where the engine is the first element, at position zero. The caboose is the last element, at position length - 1
    So an array of 12 elements would have the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

    myArray[2] would get the third element of the array (0, 1, 2...)

    Decide which word (realistically which index of the array) you want to print, and print it, outside the loop.
    Search keywords: java array access for more details on arrays.
    Show the modified code if you have questions remaining, and remember code tags! Good luck

    --- Update ---

    Please do not post your question multiple times.
    Threads merged

Similar Threads

  1. Replies: 9
    Last Post: March 18th, 2013, 05:49 PM
  2. How to print numbers using do while, for loop and if else statement?
    By techlover in forum Loops & Control Statements
    Replies: 1
    Last Post: October 30th, 2012, 02:13 PM
  3. Replies: 16
    Last Post: September 16th, 2012, 11:59 PM
  4. Can't figure out how to print out number of lines and words in a file in c++
    By javapenguin in forum Other Programming Languages
    Replies: 1
    Last Post: January 29th, 2012, 06:53 PM
  5. Please help i cant figure it out - problems with Location and Line
    By tuts73 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2012, 07:58 PM