Advertisement
s_m4rt

Untitled

Nov 10th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 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. if (slowo.length()!=slowo2.length()){
  39. JOptionPane.showMessageDialog(null, "Slowa nie sa anagramami");
  40. break;
  41. }
  42. for(int i=0; i<slowo.length(); i++){
  43. char l = slowo.charAt(i);
  44. litery.add(l);
  45. }
  46. Collections.sort(litery);
  47. for(int i=0; i<slowo2.length(); i++){
  48. char l = slowo2.charAt(i);
  49. litery2.add(l);
  50. Collections.sort(litery2);}
  51. if (litery.equals(litery2)){
  52. JOptionPane.showMessageDialog(null, "Slowa sa anagramami");
  53. }
  54. else {
  55. JOptionPane.showMessageDialog(null, "Slowa nie sa anagramami");
  56. }
  57. break;
  58.  
  59. }
  60.  
  61. }}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement