Advertisement
Kushtrim

PatternMatching2

Jun 9th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import javax.swing.*;
  2. /**
  3.  * Write a description of class PatternMatching here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. public class PatternMatching
  9. {
  10.     public static void main (String[] args )
  11.     {
  12.         String fjala  = JOptionPane.showInputDialog("Shkruani fjalen fillestare : ");
  13.         String mostra  = JOptionPane.showInputDialog("Shkruani mostren qe do te kerkohet");
  14.         int gjatesiaEMostres = mostra.length();
  15.         int index = -1;
  16.  
  17.         if ( gjatesiaEMostres > fjala.length() )
  18.         {
  19.             JOptionPane.showMessageDialog(null,"Gjatesia e mostres eshte me e madhe se fjala fillestare");
  20.             //  System.exit(0);
  21.         }
  22.         else
  23.         {
  24.             String hapesira = "";
  25.             for ( int i = 0 ;  i<= fjala.length() - gjatesiaEMostres; i++)
  26.             {
  27.                 String sub =  fjala.substring(i,i+gjatesiaEMostres).toUpperCase();
  28.                 // po e konvertojme ne fun mostren dhe fjaline ne shkronja te medhaja, ne menyre qe algoritmi per gjetjen e pozites se mostres ne fjali te mos jetë case-sensitive
  29.                 if ( mostra.toUpperCase().equals(sub))
  30.                 {
  31.                     index = i;
  32.  
  33.                     // tash e printojme edhe fjaline, se bashku me mostren, nen te ne poziten ku eshte gjetur
  34.                     System.out.println(fjala);
  35.                     System.out.println(hapesira + mostra);
  36.  
  37.                     break; // sepse u gjet
  38.                 }
  39.                 else {
  40.                     hapesira += " ";
  41.                 }
  42.             }
  43.             JOptionPane.showMessageDialog(null,"Pozita : " + (index));
  44.         }
  45.  
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement