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

public class Reverseing
{  
  public static void main(String[] args) throws Exception
  {  
    File f = new File("backwards.txt");
     Scanner finput = new Scanner(f);
    String sentence = "", s = "", r = ""; 
    while(finput.hasNext()) 
        {
            sentence = s + finput.nextLine();
        } 
              ArrayList<String> lines = new ArrayList<String>();
              ArrayList<String> words = new ArrayList<String>();
         while (input.hasNextLine()) {
            line = input.nextLine();
         }
                for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            sum += s.length();
                }
    String[] toks = sentence.split(" "); 
    for(int i = toks.length - 1 ; i >= 0; i--) { 
            r = r + toks[i] + " "; 
        }
        System.out.println(r); 
    }
}

program to reverse the lines of a file and also to reverse the order of the words in each line of the file. Use an ArrayList for the lines of the file and for the words of each line. Print out the lines to demonstrate that the lines and each word of each line have been reversed.
Example- "This is a test." should be reversed to read: "test. a is This"

Recommended Answers

All 6 Replies

private String reverse_word(String word) {
String new_word = "";
for(int i=word.length()-1; i>=0; i--) {
new_word += word.charAt(i);
}
return new_word;
}

private String reverse_sentence(String sentence) {
String new_sentence = "";
String[] toks = sentence.split(" "); 
for(int i=toks.length-1; i>=0; i--) {
new_sentence += " " + reverse_word(toks[i]);
}
return new_sentence;
}

// sentence (line) = "Bla bla bla..."
// reversed sentence = reverse_sentence(sentence);

The code is not tested, but should be works...

Or If you want, you can use StringBuffer

StringBuffer buffer = new StringBuffer(word); // or sentence
reversed_word = buffer.reverse().toString(); // or reversed_sentence
import java.io.*;
import java.util.*;

public class Homework10 {

Scanner input = new Scanner(new File("backwards.txt"));

private String reverse_word(String word) {
String new_word = "";
for(int i=word.length()-1; i>=0; i--) {
new_word += word.charAt(i);
}
return new_word;
}

private String reverse_sentence(String sentence) {
String new_sentence = "";
String[] toks = sentence.split(" "); 
for(int i=toks.length-1; i>=0; i--) {
new_sentence += " " + reverse_word(toks[i]);
}
return new_sentence;
}
}

i seem to be missing something

public final class StringUtils {
     private static final SPACE = " ";

     public static String reverseSentence( String sentence ) {
        StringBuilder builder = new StringBuilder();
         for ( String sentencePart : sentence.split(SPACE) ) {
             builder.append(sentencePart)
                        .append(SPACE);
         }

         return builder.reverse().toString();
     }

    public static String reverse( String input ) {
          return String.valueOf( reverse( input.toCharArray() ) );
    }

    public static char[] reverse( char[] input ) {
        char[] result = new char[input.length];
        for( int i = input.length; i > 0; i-- ) {
            result[i] = input[i];
        }

        return result;
    }
}

Or it can be done quite elegantly using recursion. Easy enough to add the words and lines to an ArrayList too as that seems to be part of the requirement:

private static void reverseLines( Scanner input)
  {
     while( input.hasNextLine() )
     {
      String line = input.nextLine();
      reverseLines( input );
      reverseWords( new Scanner( line ) );
      System.out.println();
     
     }
  }
  
  private static void reverseWords( Scanner line )
  {
     while( line.hasNext() )
     {
        String word = line.next();
        reverseWords( line );
        System.out.print( word + " ");
     }
        
  }
import java.io.*;
import java.util.*;

public class Reverseing
{ 
public static void main(String[] args) throws Exception
{ 
File f = new File("backwards.txt");
Scanner finput = new Scanner(f);
String sentence = "", s = "", r = ""; 
while(finput.hasNext()) 
{
sentence = s + finput.nextLine();
} 
ArrayList<String> lines = new ArrayList<String>();
ArrayList<String> words = new ArrayList<String>();
while (input.hasNextLine()) {
line = input.nextLine();
}
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
sum += s.length();
}
String[] toks = sentence.split(" "); 
StringBuffer buf;
for(int i = toks.length - 1 ; i >= 0; i--) { 
buf = new StringBuffer(toks[i]);
r += buf.reverse().toString() + " "; // reverse word
}
System.out.println(r); 
}

when compiled says cannot find symbol how would i fix

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.