Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. /*
  2. Program 21
  3. Lies, Damned Lies, and Statistics
  4. Due 4/21/19
  5.  
  6. */
  7. import javax.swing.*;
  8. import java.util.*;
  9.  
  10. public class Program21{
  11.         public static void main(String[] args){
  12.            
  13.  
  14.             String nSentence = "";
  15.             nSentence = JOptionPane.showInputDialog("Please enter a Sentence");
  16.             System.out.println(nSentence);
  17.             char[] aSentence = new char[nSentence.length()];
  18.             ArrayList<String> brandNewSentence = new ArrayList<String>();
  19.            
  20.             String word = "";
  21.             String space = " ";
  22.  
  23.             for (int i = 0; i < nSentence.length() ; i++){
  24.                
  25.                 aSentence[i] = nSentence.charAt(i);
  26.                 if(aSentence[i] != ' ' ){
  27.                     word = word + aSentence[i];
  28.                     System.out.println("||" + word);
  29.                    
  30.                 }else if(aSentence[i] == ' '){
  31.                     if(i == 0 ){
  32.                     brandNewSentence.add(space);
  33.                     }else
  34.                    
  35.                     if(aSentence[i-1] != ' '){
  36.                         brandNewSentence.add(word);
  37.                        
  38.                         brandNewSentence.add(space);
  39.                         word = "";
  40.                     }
  41.                 } if(i == nSentence.length() - 1  && !(word.equals("")) ){
  42.                    
  43.                     brandNewSentence.add(word);
  44.                    
  45.                 }  
  46.                  
  47.             }
  48.             System.out.println(brandNewSentence);
  49.            
  50.             /*
  51.             for (int i = 0; i < aSentence.length - 1; i++){
  52.  
  53.                 System.out.println(aSentence[i]);
  54.             }
  55.             */
  56.         }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement