Guest User

Untitled

a guest
May 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. /**
  2. *
  3.  
  4. import java.util.Arrays;
  5.  
  6. public class Main {
  7. public static String[] wordList(String line){
  8. return line.split(" ");
  9. }
  10.  
  11. public static void main(String args[]) {
  12. String words = "test words tesing";
  13. String[] arr = wordList(words);
  14. for(words i=0; i<words.length; i++)
  15. for (String s: arr)
  16. System.out.println(s);
  17. }
  18.  
  19. }
  20.  
  21. */
  22.  
  23. import java.util.Scanner;
  24. import java.util.Arrays;
  25.  
  26. public class Main {
  27.  
  28. public static void main ( String args[] ){
  29. String[] sEnterWord = getSortedWordArr();
  30. showWordlist(sEnterWord);
  31. String sWordToChange = getInputFromKeyboard("Which word would you like to change? ");
  32. System.out.println("You have chosen to change the word : " + sWordToChange);
  33. changeWordInArray(sWordToChange, sEnterWord);
  34. Arrays.sort(sEnterWord);
  35. showWordlist(sEnterWord);
  36. }
  37.  
  38. private static String[] getSortedWordArr(){
  39. String line = getInputFromKeyboard("How many words are you going to enter? ");
  40. int length = Integer.valueOf(line);
  41.  
  42. String[] sEnterWord = new String[length];
  43.  
  44. for(int nCtr = 0; nCtr < length; nCtr++){
  45. sEnterWord[nCtr] = getInputFromKeyboard("Enter word " + (nCtr+1) + ":");
  46. }
  47.  
  48. Arrays.sort(sEnterWord);
  49.  
  50. return sEnterWord;
  51. }
  52.  
  53. private static String getInputFromKeyboard(String prompt){
  54. System.out.print(prompt);
  55. Scanner s = new Scanner(System.in);
  56. String input = s.nextLine();
  57.  
  58. return input;
  59. }
  60.  
  61. private static void showWordlist(String[] words){
  62. System.out.println("Your words are: ");
  63. for (String w : words){
  64. System.out.println(w);
  65. }
  66. }
  67.  
  68. private static void changeWordInArray(String word, String[] array){
  69. String newWord = getInputFromKeyboard("Enter the new word: ");
  70.  
  71. for (int i = 0; i < array.length; i++){
  72. if (array[i].equals(word)){
  73. array[i] = newWord;
  74. break;
  75. }
  76. }
  77.  
  78. Arrays.sort(array);
  79. }
  80. }
Add Comment
Please, Sign In to add comment