Simple Java Question

Questions about programming languages and debugging
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1602
Joined: 28 Aug 2005, 16:00
15

Simple Java Question

Post by maboroshi »

Did some one say Java

Hi everyone I am embarking on the path of the awesome language known as Java

and need some help or guidance rather

The code I am working with inputs a textfile and reverses characters in the file however I can't figure out how to instead of reversing characters reverse words

Any suggestions would be appreciated

Code: Select all


import java.io.*;
import java.lang.Object.*;

public class fileIT {
    public static void main(String[] args) throws IOException {
	BufferedReader inputFile = new BufferedReader(new FileReader("C:\\JavaCH\\helloworld.txt"));
	String str;        

	while ((str = inputFile.readLine()) != null) {
	StringBuffer theend = new StringBuffer(str);
	StringBuffer reverse = theend.reverse();
	System.out.println(reverse);
	}
    }
}
 

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11532
Joined: 06 Apr 2005, 16:00
15
Location: The zone.
Contact:

Post by bad_brain »

Well, I first thought about turning the charakters into ASCII-numbers, read them in and....umm...then I lost the plan....:lol:
but well, a good old array will do it too:

Code: Select all

public static String reverse(String s) {
        char[] carray=s.toCharArray();
        char[] carray2=new char[carray.length];
        for(int i=carray.length-1, j=0; i>=0; i--, j++) {
            carray2[j]=carray[i];
        }
        return new String(carray2);
    }

:wink:

Post Reply