Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.io.FileReader;
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5.  
  6. class Lab1 {
  7. public static void sort(int[] a) {
  8. int temp;
  9. boolean repeatCheck = true;
  10. do {
  11. repeatCheck = false;
  12. for (int i = 0; i < a.length - 1; i++) {
  13. temp = a[i + 1];
  14. if (a[i] > a[i + 1]) {
  15. a[i + 2] = a[i];
  16. a[i] = temp;
  17. repeatCheck = true;
  18. continue;
  19. }
  20. }
  21. } while (repeatCheck);
  22. }
  23.  
  24. public static void main(String[] args) throws Exception {
  25. String filePath = args[0];
  26. File textFile = new File(filePath);
  27. if (args.length == 0) {
  28. System.out.println("ingen filnamn specifierat");
  29. }
  30. else if (!textFile.isFile()) {
  31. System.out.println("filen hittades ej");
  32. }
  33. else {
  34.  
  35. FileReader file = new FileReader(filePath);
  36. BufferedReader reader = new BufferedReader(file);
  37.  
  38. String numString = "";
  39. String currentLine = reader.readLine();
  40.  
  41. while (currentLine != null) {
  42. numString += currentLine;
  43. currentLine = reader.readLine();
  44. }
  45.  
  46. String[] strArr = numString.split(" ");
  47. int[] numArr = new int[strArr.length];
  48.  
  49. for (int i = 0; i < strArr.length; i++) {
  50. numArr[i] = Integer.parseInt(strArr[i]);
  51. }
  52.  
  53. sort(numArr);
  54. System.out.println("Sorted array: " + Arrays.toString(numArr));
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement