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 5 of 5

Thread: Word Scramble Program Help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Word Scramble Program Help

    Hi I need some help with this project I'm working on. I need to write a program which scrambles the intermediate letters of words while keeping the first and last letters the same. The paragraph comes from a .txt file and it is three lines long. I have been able to successfully scramble the first line of the file, but the next two lines aren't showing up. My code I have written is below, any help would be greatly appreciated.

    Scanner infile = new Scanner( new FileReader( args[0] ) );
    PrintWriter outfile = new PrintWriter( args[1] );

    int pos, start=0;

    char first, last;
    String text = infile.nextLine(); //<<< I think my problem has something to do with this...

    Random generator = new Random();

    while (text.length()>0){
    int whiteSpace = text.indexOf(" ", start);
    if (whiteSpace<0){break;} else{

    first = text.charAt(start);
    last = text.charAt(whiteSpace-1);

    if ((whiteSpace-1)-start <=0)
    System.out.print(text.charAt(start)+ " ");
    else {
    String eachWord = text.substring(start+1 , whiteSpace-1) ;
    //this will give me the substring to scramble, need a integer to randomize?

    System.out.print(first);
    // this is the first character for each word. need the last and scramble the middle

    while(eachWord.length()>0){
    pos = generator.nextInt(eachWord.length()); //outputs a random value of the size
    System.out.print(eachWord.charAt(pos));
    // onces i choose that racndom character i need to take it out of the list.
    if (pos==0)
    eachWord = eachWord.substring(1);
    else if (pos == eachWord.length()-1 )
    eachWord = eachWord.substring(0,pos);
    else
    eachWord = eachWord.substring(0,pos) + eachWord.substring(pos+1, eachWord.length());
    }
    System.out.print(last + " ") ;//this is the last charater of each word!
    }
    start = whiteSpace+1;

    }}

    int lastWhiteSpace = text.lastIndexOf(" ");
    System.out.print(text.charAt(lastWhiteSpace+1));
    String lastWord = text.substring(lastWhiteSpace+2 , text.length()-2);
    while (lastWord.length()>0){
    int lpos = generator.nextInt(lastWord.length());
    System.out.print(lastWord.charAt(lpos));
    if (lpos==0)
    lastWord = lastWord.substring(1);
    else if (lpos==lastWord.length()-1)
    lastWord = lastWord.substring(0,lpos);
    else
    lastWord = lastWord.substring(0,lpos)+lastWord.substring(lpos +1,lastWord.length());
    }
    System.out.print(text.charAt(text.length()-2)+".");



    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Word Scramble Program Help

    Quote Originally Posted by beginneratjava View Post
    ...I have been able to successfully scramble the first line of the file, but the next two lines aren't showing up....
    Put print statements just before and just after the statement in your code that reads the first line.
    Put print statements just before and just after the statement in your code that reads the second line.
    Put print statements just before and just after the statement in your code that reads the third line.

    Here, I'll start:

    .
    .
    .
            // Presumably this is in main() 
            Scanner infile = new Scanner(new FileReader( args[0]));
            PrintWriter outfile = new PrintWriter(args[1]);
     
            int pos, start=0;
     
            char first, last;
            System.out.println("Reading first line from " + args[0]);
            String text = infile.nextLine(); //<<< I think my problem has something to do with this...
            System.out.println("First line:\n" + text + "\nAnd now, on with the show...\n");

    Here is my file inwords.txt:

    The quality of mercy is not strained.
    It droppeth as the gentle rain from heaven
    Upon the place beneath: it is twice blest;
    It blesseth him that gives and him that takes



    And here's the output from a run with inwords.txt as the first argument on the java command line:


    Reading first line from inwords.txt
    First line:
    The quality of mercy is not strained.
    And now, on with the show...

    The qitualy of mecry is not sarnetid.



    Cheers!

    Z
    Last edited by Zaphod_b; October 16th, 2012 at 03:57 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Word Scramble Program Help

    That doesn't really help me. I can't have that extra stuff printed in my output, and my original code already scrambles the first line, the problem is that it stops there and doesn't do anything to my next lines of text, they don't show up at all, not even unscrambled they just don't show up in my output.

  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: Word Scramble Program Help

    I can't have that extra stuff printed in my output,
    You can always remove it after you use it to see what is going on.



    and my original code already scrambles the first line, the problem is that it stops there and doesn't do anything to my next lines of text, they don't show up at all, not even unscrambled they just don't show up in my output.
    The sooner you figure out what the code is doing, the sooner you can start to make changes to the code.

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Word Scramble Program Help

    Quote Originally Posted by beginneratjava View Post
    .... it stops there and doesn't do anything to my next lines of text...
    Well, that was kind of the point of my suggestion: Where in your program to you read more than one line? The program won't read more than one line unless and until you tell it to.

    I mean, if you can't find the statement in your program that reads the second line (and the third and so forth), you have to put such statements in the code. These programs don't write themselves according to your intent, right? They do what you tell them to.

    Bottom line: Generally speaking, with a Scanner you will need a loop of some kind to read all of the lines in a file. Look at documentation and tutorial material on use of Scanner methods to do the deed.



    Cheers!

    Z
    Last edited by Zaphod_b; October 18th, 2012 at 01:55 PM.

Similar Threads

  1. Replies: 5
    Last Post: August 20th, 2012, 01:01 AM
  2. pls help me with word occurances program
    By kashif in forum Collections and Generics
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  3. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  4. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2011, 09:39 AM
  5. [SOLVED] Word Scramble Using Objects & Methods
    By Whitechapel in forum Object Oriented Programming
    Replies: 3
    Last Post: May 23rd, 2010, 12:38 PM

Tags for this Thread