[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reversing String Elements of an Array List

Posted on 2011-04-25
14
Medium Priority
?
408 Views
Last Modified: 2013-11-23
I have to read an input file and reverse each line while keeping the lines in the same order, then output to the same file. I thought the best way to do this was to extract each line into an Array List, which I've done... But I can't figure out how to reverse the elements of the array list. If I were just reversing the order of the array list it would be easy... Any help is greatly appreciated, I have my input and output set... just missing the reverse method...Any help is GREATLY appreciated.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;

public class ReverseLines {

  public static void main(String[] args) {
    try {
      BufferedReader input = new BufferedReader(new FileReader("java.txt"));
      ArrayList list = new ArrayList();
      String line;
      while ((line = input.readLine()) != null) {
        list.add(line);
      }
      input.close();


      PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("java.txt")));
      for (Iterator i = list.iterator(); i.hasNext();) {
        output.println((String) i.next());
      }
      output.close();
    } 
    catch (IOException e) {
      System.out.println(e);
    }
  }
}

Open in new window

0
Comment
Question by:ThaRaven
  • 5
  • 4
  • 4
  • +1
14 Comments
 
LVL 20

Expert Comment

by:a_b
ID: 35464846
Use a stack to insert the lines and then pop elements of the stack into the arraylist.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35464847

public static void reverse(List<?> list)

method of Collections class will do it for you
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ThaRaven
ID: 35464848
Thanks, but I'm not trying to reverse the order of the array list, I'm trying to reverse each LINE while keeping the lines in the same order...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35464852
Make arraylist of each charcater of the lile and then reverse
them
0
 
LVL 92

Expert Comment

by:objects
ID: 35464858
0
 

Author Comment

by:ThaRaven
ID: 35464859
I understand how to reverse a single string... what I do not understand, is how to reverse a string, and keep it in the array list so that I can put it into the output file.
0
 
LVL 92

Expert Comment

by:objects
ID: 35464862
And you don't need an array list. you can read each line and reverse it in the one loop
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35464864

ArrayList ls = new ArrayList();

for(int j=0; j< line.linegth(); j++){
ls.add(new Character(line.charAt(j));
}

Collections.reverse(ls);
line = '';
for(int j=0; j<ls.size(); j++){line += (Charcater) ls.get(j));



0
 
LVL 47

Expert Comment

by:for_yan
ID: 35464869
You read it and as you read it you reverse and out put it
if the lines do not need to change the order
0
 
LVL 92

Expert Comment

by:objects
ID: 35464870
     while ((line = input.readLine()) != null) {
        StringBuilder sb = new StringBuilder(line);
        sb.reverse();
        output.println(sb.toString());
      }
0
 

Author Comment

by:ThaRaven
ID: 35464872
I was using an arraylist because my input and output files are the same..
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 35464887
then do the reversing in the output loop
or put the reversed string in the list


     while ((line = input.readLine()) != null) {
        StringBuilder sb = new StringBuilder(line);
        sb.reverse();
        list.add(sb.toString());
      }
0
 

Author Closing Comment

by:ThaRaven
ID: 35464904
Thank you so much!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question