This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Java help

By: a guest on Apr 6th, 2013  |  syntax: Java  |  size: 1.95 KB  |  views: 64  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.*;
  2. import java.io.*;
  3. public class Reversor {
  4.  
  5.     private ArrayList<String> sentence;
  6.     private ArrayList<String> newSentence;
  7.     String word;
  8.     String w;
  9.  
  10.     public void getSentence() throws IOException {
  11.        sentence = new ArrayList<String>();
  12.         Scanner fileScan = new Scanner (new File("data/sentence.txt"));
  13.            while(fileScan.hasNext()){
  14.             String word= fileScan.next();
  15.             sentence.add(word);
  16.             }
  17.            System.out.println(sentence);
  18.        }
  19.  
  20.     public void reverseEachWord(){
  21.         newSentence = new ArrayList<String>();
  22.         for (String word : sentence) {
  23.             Stack reverseWordStack = stackChars(word);
  24.             String reverseWord = unStackChars(reverseWordStack);
  25.             newSentence.add(reverseWord);
  26.             System.out.println(newSentence);
  27.         }
  28.     }
  29. private Stack<Character> stackChars(String word){
  30.  
  31.         Stack reverseWordStack = new Stack<Character>();
  32.         for (int i=0; i<word.length(); i++){
  33.             reverseWordStack.push(word.charAt(i));
  34.  
  35.         }
  36.          return reverseWordStack;
  37.     }
  38.  
  39.     private String unStackChars(Stack<Character> chars) {
  40.         String newWord = new String();
  41.         while (!chars.isEmpty()) {
  42.  
  43.             newWord += chars.pop();
  44.         }
  45.         return newWord;
  46.     }
  47.  
  48.     public String toString() {
  49.         StringBuffer result = new StringBuffer(" ");
  50.         for (String word : newSentence){
  51.             result.append(" ");
  52.             result.append(word);
  53.         }
  54.         return result.toString();
  55.     }
  56. }
  57.  
  58. import java.io.IOException;
  59. public class Driver {
  60.     public static void main(String[] args){
  61.         Reversor r= new Reversor();
  62.         try {
  63.             r.getSentence();
  64.             r.reverseEachWord();
  65. //            r.stackChars();
  66.             r.toString();
  67.         } catch (IOException e) {
  68.             e.printStackTrace();
  69.         }
  70.     }
  71.  
  72. }
clone this paste RAW Paste Data