Results 1 to 4 of 4
  1. #1
    ScaryJello is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default Problem with ordering for loops

    For an assigment, I have to read in a text document, reverse the lines, to the last line becomes the first and etc. then i have to take the words in the line and reverse them as well. It seems like everything should work with my program, however, it refuses to make the words backwards on the last line(which is actually the first line). another example is if there is only one line of text in the document, it just prints it out, it does not take the words and reverse them.

    can someone please help me debug this problem? it seems like my forloops or prints or in the wrong spot
    Attached Files Attached Files

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I couldn't get your link to work. Maybe you can post your code.

  3. #3
    ScaryJello is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

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



    public class Shell
    {
    public static void main(String[] args)
    {

    Scanner console = new Scanner(System.in);
    System.out.println("Enter the input file name: ");
    String inputFileName = console.next();
    System.out.println("Enter the output file name: ");
    String outputFileName = console.next();

    ArrayList<String> lines = new ArrayList<String>();
    ArrayList<String> words = new ArrayList<String>();

    try
    {

    Scanner in = new Scanner(new File(inputFileName));
    PrintStream out = new PrintStream(new File(outputFileName));


    while(in.hasNextLine())
    {
    String tempLine = in.nextLine();
    lines.add(tempLine);
    while(in.hasNext())
    {
    String token = in.next();
    words.add(token);
    }

    }

    for(int x = words.size() - 1; x >= 0; x--)
    {
    out.println(words.get(x));
    }

    for(int x = lines.size() - 1; x >= 0; x--)
    {
    out.println(lines.get(x));
    }






    in.close();
    out.close();
    }




    catch(FileNotFoundException e)
    {
    System.err.println("Cannot find input file " );
    System.exit(1);
    }
    catch(IOException e)
    {
    System.err.println("Cannot open input/output file " );
    System.exit(2);
    }


    }
    }

    ANY IDEAS??

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One way (semi-pseudocode):
    Java Code:
    while scanner has next line
      get next line
      split line on whitespace "\\s+" into array of String tokens
      create StringBuilder
      for loop iterating through token array *backwards*
        append each token and a space " " into StringBuilder
      end backwards for loop
      add StringBuilder object.toString() into lines ArrayList at index position 0
    end while loop
    for each line in lines arraylist
      println into output file
    end for
    Last edited by Fubarable; 03-31-2009 at 09:38 PM.

Similar Threads

  1. [HELP]Java loops
    By jude113 in forum JCreator
    Replies: 0
    Last Post: 03-06-2009, 04:11 AM
  2. Question about loops
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 06:40 PM
  3. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 04:59 AM
  4. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •