Advertisement
s_m4rt

Untitled

Nov 10th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. package litery;
  2.  
  3. import java.awt.List;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Collections;
  7.  
  8. import javax.swing.JOptionPane;
  9.  
  10. public class Main {
  11.  
  12. public static void main(String[] args) {
  13. String slowo, slowo2;
  14. int wybor=Integer.parseInt(JOptionPane.showInputDialog("Co chcesz zrobic?\n1.Sprawdzic czy slowo jest palindromem\n2.Sprawdzic czy slowa sa anagramami"));
  15. while(wybor!=1 && wybor!=2){
  16. if(wybor!=1 && wybor!=2) {
  17. wybor=Integer.parseInt(JOptionPane.showInputDialog("Wybrales niepoprawna opcje. \n1. Sprawdzic czy slowo jest palindromem\n2.Sprawdzic czy slowa sa anagramami"));}}
  18. switch(wybor){
  19. case 1: {
  20. slowo=JOptionPane.showInputDialog("Podaj slowo, ktore chcesz sprawdzic czy jest palindromem");
  21. slowo=slowo.toLowerCase();
  22. slowo2=new StringBuilder(slowo).reverse().toString();
  23. boolean czy=new String(slowo).equals(slowo2);
  24. if(czy==true){
  25. JOptionPane.showMessageDialog(null, "Slowo jest palindromem");
  26. }
  27. else {
  28. JOptionPane.showMessageDialog(null, "Slowo nie jest palindromem");
  29. }
  30. break; }
  31. case 2: {
  32. slowo=JOptionPane.showInputDialog("Podaj 1 slowo, ktore chcesz sprawdzic czy jest anagramem");
  33. slowo2=JOptionPane.showInputDialog("Podaj 2 slowo, ktore chcesz sprawdzic czy jest anagramem");
  34. slowo=slowo.toLowerCase();
  35. slowo2=slowo2.toLowerCase();
  36. ArrayList litery = new ArrayList();
  37. ArrayList litery2 = new ArrayList();
  38. for(int i=0; i<slowo.length(); i++){
  39. char l = slowo.charAt(i);
  40. Character.toLowerCase(l);
  41. litery.add(l);
  42. }
  43. Collections.sort(litery);
  44. for(int i=0; i<slowo2.length(); i++){
  45. char l = slowo2.charAt(i);
  46. Character.toLowerCase(l);
  47. litery2.add(l);
  48. Collections.sort(litery2);}
  49. if (litery.equals(litery2)){
  50. JOptionPane.showMessageDialog(null, "Slowa sa anagramami");
  51. }
  52. else {
  53. JOptionPane.showMessageDialog(null, "Slowa nie sa anagramami");
  54. }
  55. break;
  56.  
  57. }
  58.  
  59. }}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement