Guest User

Untitled

a guest
Jul 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package javaapplication1;
  6.  
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. /**
  14.  *
  15.  * @author richard
  16.  */
  17. public class JavaApplication1 {
  18.  
  19.     /**
  20.      * @param args the command line arguments
  21.      */
  22.    
  23.         private static String readFileAsString(String filePath) throws java.io.IOException{
  24.             byte[] buffer = new byte[(int) new File(filePath).length()];
  25.             FileInputStream f = new FileInputStream(filePath);
  26.             f.read(buffer);
  27.             return new String(buffer);
  28.         }
  29.    
  30.     public static void main(String[] args) {
  31.         try {
  32.             // TODO code application logic here
  33.             String file = readFileAsString("C:\\Test.txt");
  34.             String[] arr = file.split("#");
  35.            
  36.             int i = 0;
  37.             for(String zeile:arr){
  38.                 i++;
  39.                 System.out.printf("Zeile %d: %s\n",i,zeile);
  40.             }
  41.            
  42.             //Zufällige Zeile bestimmen
  43.             int zeile = (int)((arr.length)*Math.random());
  44.             System.out.printf("\n\nZufällige Zeile Nummer: %d\n%s",zeile,arr[zeile]);
  45.            
  46.             String deineZeile = arr[zeile];
  47.             char[] zeichen = deineZeile.toCharArray();
  48.            
  49.             int tauschA = (int)((zeichen.length)*Math.random());
  50.             int tauschB = (int)((zeichen.length)*Math.random());
  51.            
  52.             char buff = zeichen[tauschA];
  53.             zeichen[tauschA] = zeichen[tauschB];
  54.             zeichen[tauschB] = buff;
  55.             System.out.printf("\n\nVertausche Zeichen %d mit Zeichen %d: \n%s",tauschA,tauschB, new String(zeichen));
  56.         } catch (IOException ex) {
  57.             Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
  58.         }
  59.        
  60.     }
  61. }
Add Comment
Please, Sign In to add comment