# Shkenca > Informatikë dhe Internet > Arti i programimit >  Java - Vazhdimesi e programit...

## mad

Ahoj! serish me nje detyre ne Java.
eshte po i njejti program si me pare, i njejti funksion, vetem se kerkohet te paraqitet ne menyre grafike, dmth me JFileChooser, dhe JPanel, pra si Applet!

ja ku eshte kodi i gjithe programit, vetem se kur mundohem ta kompiloj, me jep nje error message, te cilin jam munduar ta korrigjoj, po pa asnje rezultat. mbi te gjitha, ajo qe shkruhet ne error, eshte pa kuptim.



```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import java.util.*;


public class GuiParser extends JFrame {
    private class ExtendedTA extends JTextArea{ 
              void prnMap(java.util.Map map){
	java.util.Iterator it = map.entrySet().iterator();
	java.util.Map.Entry polozka;
	setText("");
	while(it.hasNext()){
	   polozka = (java.util.Map.Entry)it.next();
	   append(" "+polozka.getKey()+": "+polozka.getValue());
	}
               }
    }
    class Kl extends KeyAdapter { //obsluhuje stisknuti Enter
          public void keyPressed (KeyEvent e) {
	if (e.getKeyChar()==KeyEvent.VK_ENTER) {
	    processFile(txtSoubor.getText());
	}
          }
    }
	
    class AkceBtnOK implements ActionListener { 
          public void actionPerformed (ActionEvent e) {
	processFile(txtSoubor.getText());
          }
    }
	
    class AkceBtnFile implements ActionListener { 
          public void actionPerformed (ActionEvent e) {
	JFileChooser fc = new JFileChooser();
	int stavVyberu = fc.showOpenDialog(GuiParser.this);
	if (stavVyberu ==JFileChooser.APPROVE_OPTION){
	      txtSoubor.setText(""+fc.getSelectedFile());
	}
          }
    }
	
    private void processFile(String soubor){ 
           try{
	vypis.prnMap(procesDomen(soubor)); 
           }
           catch (IOException e) {
	JOptionPane.showMessageDialog(null,"Chyba pri ctení - soubor
pravdepodobně neexistuje", "Soubor neexistuje",
JOptionPane.ERROR_MESSAGE);           }    }
    

	private JPanel zahlavi = new JPanel();
	private JLabel lblZadej = new JLabel ("Zadej soubor: ");
  	private JButton btnOK = new JButton("OK");
	private JButton btnFile = new JButton("Prochazet");
  	private JTextField txtSoubor = new JTextField(20);
  	private ExtendedTA vypis = new ExtendedTA();
	
	public GuiParser(String nazev) { //konstruktor
		super(nazev);
		addWindowListener(new WindowAdapter () {
			public void windowClosing (WindowEvent e) {
				System.exit(0);
			}
		});
		zahlavi.add(lblZadej);
		zahlavi.add(txtSoubor);
		zahlavi.add(btnFile);
		zahlavi.add(btnOK);
		getContentPane().add(zahlavi,BorderLayout.NORTH);
		getContentPane().add(new JScrollPane(vypis));
		txtSoubor.addKeyListener(new Kl());
		btnOK.addActionListener(new AkceBtnOK());
		btnFile.addActionListener(new AkceBtnFile());
	}	
	public static TreeMap procesDomen(String NazevSouboru) {
								
	  try {
	     BufferedReader read = new BufferedReader(new FileReader(NazevSouboru));	     String line;
	     TreeMap map = new TreeMap();
			
	     while ( (line = read.readLine()) != null) {
	        StringTokenizer radek = new StringTokenizer(line," ");
		String adresa="";
	                String domena="";
	                String pocet="";
						
		while (radek.hasMoreTokens()) {
		     adresa =radek.nextToken();
		     String pomlcka1 =radek.nextToken();
		     String pomlcka2 =radek.nextToken();
		     String datum =radek.nextToken();
                    	     String cesta =radek.nextToken();
                    	     pocet =radek.nextToken();
                    	}
                    	
                    	StringTokenizer stAdresa = new StringTokenizer(adresa,".");                    		
                    	while (stAdresa.hasMoreTokens()) {
                    	    domena=stAdresa.nextToken();
                    				 }
                    	    if (map.containsKey(domena)) {
                    	       if (pocet.equals("-")){
                    	           pocet="0";}
                    	           int volba=0;
                    	           Object Objekt =map.get(domena);
                    	           int howBig = ((Integer) Objekt).intValue();
                    	           howBig+=Integer.valueOf(pocet).intValue();
                    	       try {
                    	         volba=Integer.valueOf(domena).intValue();
                    	       }
                    	       catch (NumberFormatException e3) {
                    		map.put(domena,new Integer(howBig));
                    	       }
                    				
                    	}
                    	else {
                    	      int volba=0; 
                    	      if (pocet.equals("-")) {
                    	           pocet="0";
                    	      }
                    	      try {
                    	           volba=Integer.valueOf(domena).intValue();
                    	      }
                    	      catch (NumberFormatException e3) {
                    		map.put(domena,new Integer(pocet));
                    	      }
                    	}
                          }
                          return map;
                   }
                   catch (FileNotFoundException  e1) {
                      System.out.println("Dany Subor "+NazevSouboru+" nenalezen");
                      System.exit(1);
                   }
                   catch (IOException e2) {
                   	System.out.println("Nastala chyba pri citani");
                   	System.exit(2);
                   }
                   catch (NumberFormatException e3) {
                   	System.out.println("nastala niekde chyba");
                   }
                   
                   return null;
              }
                
                
	public static void ukoncit() {
		
		/* Class  */

            System.out.println("Na ukoncenie stlacte tlacitko z klavesnice");
            try {
                InputStreamReader klavesa = new InputStreamReader(System.in);
                klavesa.read();
		}
            catch (IOException e) {
	System.out.println("Nastala chyba pri zadani znaku z klavesnice.");
	System.exit(1);
            }	
      }                   
                    		
       public static void VypisMapy (TreeMap map)	{
		
	/* Vypis z mapy */
		
	System.out.println("Domena a dany pocet domen");
		
	Iterator iter =map.entrySet().iterator();
		
	while (iter.hasNext()) {
		Map.Entry dat = (Map.Entry)iter.next();
		String key = (String) dat.getKey(); 
		System.out.println(key+"\t\t"+dat.getValue());
	}
		
	 System.out.println("Domena a dany pocet domen");
		
	ukoncit();
			
}			
		
	 
	
	public static void main (String [] args) {
	
		TreeMap point;
		point=procesDomen(args[0]);
		
		GuiParser apl=new GuiParser("Zpracovaní ftp-logu");
			apl.setLocation(100,100);
			apl.setSize(500,400);
			apl.setVisible(true);
		
	
			point=procesDomen(args[0]);
			VypisMapy(point);
	}
}
```

H:\GuiParser.java:65: exception java.io.IOException is never thrown in body of corresponding try statement
		catch (IOException e) {
                ^
1 error

Nuk e kuptoj, pse me thote compiler-i, qe catch-i i ketushem, eshte ne trupin e try, kur kllapat e try paraardhes jane te mbyllura???
nqs keni kohe dhe durim, hidhini 1 oko(=sy), se mos jua ze gabimin.
rrofshi!

----------


## cunimartum

Ne klasen ExtendedTA  nuk hidhet (throw) IOException prandaj ti nuk ke si ta kapesh ate.
Gabimi nuk ka te beje me vendndodhjen e kllapave po me faktin qe ti mundohesh te kapesh nje Exception qe nuk hidhet.
Rreshtin ku te del gabimi ndroe ne "catch (Exception e) {"
Megjithate edhe pse kompilohet ka gabime mesa duket se s'del gje. Une s'kam as kohe as nerva te gjej gabimet keshtu qe po e mbarove hidhe njehere.

Ps. Appleti s'ka main.

----------


## mad

dmth, tek kjo klase ben "naze" vetem IOException??
metode main ka te appleti, eshte ne pjesen e fundit fare.


```
public static void main (String [] args)  {
	
	}
```

mire e ke ti, se edhe pasi kompilohet pa gabime, nuk ekzekutohet, sepse eshte nje ArrayIndexOutOfBoundsException te ky rresht:


```
TreeMap point;
		point=procesDomen(args[0]);
```

megjithate, flm per vemendjen, do mundohem ta kap gabimin! =)

----------


## 2em

> dmth, tek kjo klase ben "naze" vetem IOException??
> metode main ka te appleti, eshte ne pjesen e fundit fare.
> 
> 
> ```
> public static void main (String [] args)  {
> 	
> 	}
> ```
> ...




ne nje Applet nuk ka metode void main(..) por void init() e cila therritet nga browseri ose applet viewer qe te informoje qe appleti ist load ne system...

----------


## mad

e rregullova, e rregullova!

ja:



```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import java.util.*;


public class GuiParser extends JFrame {
    private class ExtendedTA extends JTextArea{ 
              void prnMap(java.util.Map map){
	java.util.Iterator it = map.entrySet().iterator();
	java.util.Map.Entry polozka;
	setText("");
	while(it.hasNext()){
	   polozka = (java.util.Map.Entry)it.next();
	   append(" "+polozka.getKey()+": "+polozka.getValue()+"\n");
	}
               }
    }
    class Kl extends KeyAdapter { //obsluhuje stisknuti Enter
          public void keyPressed (KeyEvent e) {
	if (e.getKeyChar()==KeyEvent.VK_ENTER) {
	    processFile(txtSoubor.getText());
	}
          }
    }
	
    class AkceBtnOK implements ActionListener { 
          public void actionPerformed (ActionEvent e) {
	processFile(txtSoubor.getText());
          }
    }
	
    class AkceBtnFile implements ActionListener { 
          public void actionPerformed (ActionEvent e) {
	JFileChooser fc = new JFileChooser();
	int stavVyberu = fc.showOpenDialog(GuiParser.this);
	if (stavVyberu ==JFileChooser.APPROVE_OPTION){
	      txtSoubor.setText(""+fc.getSelectedFile());
	}
          }
    }
	
    private void processFile(String soubor){ 
           try{
	vypis.prnMap(procesDomen(soubor)); 
           }
           catch (IOException e) {
	JOptionPane.showMessageDialog(null,"Chyba pri ctení - soubor
pravdepodobně neexistuje", "Soubor neexistuje",
JOptionPane.ERROR_MESSAGE);
           }    }
    

	private JPanel zahlavi = new JPanel();
	private JLabel lblZadej = new JLabel ("Zadej soubor: ");
  	private JButton btnOK = new JButton("OK");
	private JButton btnFile = new JButton("Prochazet");
  	private JTextField txtSoubor = new JTextField(20);
  	private ExtendedTA vypis = new ExtendedTA();
	
	public GuiParser(String nazev) { //konstruktor
		super(nazev);
		addWindowListener(new WindowAdapter () {
			public void windowClosing (WindowEvent e) {
				System.exit(0);
			}
		});
		zahlavi.add(lblZadej);
		zahlavi.add(txtSoubor);
		zahlavi.add(btnFile);
		zahlavi.add(btnOK);
		getContentPane().add(zahlavi,BorderLayout.NORTH);
		getContentPane().add(new JScrollPane(vypis));
		txtSoubor.addKeyListener(new Kl());
		btnOK.addActionListener(new AkceBtnOK());
		btnFile.addActionListener(new AkceBtnFile());
	}	
	public static TreeMap procesDomen(String NazevSouboru) {
								
	  try {
	     BufferedReader read = new BufferedReader(new FileReader(NazevSouboru));
	     String line;
	     TreeMap map = new TreeMap();
			
	     while ( (line = read.readLine()) != null) {
	        StringTokenizer radek = new StringTokenizer(line," ");
		String adresa="";
	                String domena="";
	                String pocet="";
						
		while (radek.hasMoreTokens()) {
		     adresa =radek.nextToken();
		     String pomlcka1 =radek.nextToken();
		     String pomlcka2 =radek.nextToken();
		     String datum =radek.nextToken();
                    	     String cesta =radek.nextToken();
                    	     pocet =radek.nextToken();
                    	}
                    	
                    	StringTokenizer stAdresa = new StringTokenizer(adresa,".");                    		
                    	while (stAdresa.hasMoreTokens()) {
                    	    domena=stAdresa.nextToken();
                    				 }
                    	    if (map.containsKey(domena)) {
                    	       if (pocet.equals("-")){
                    	           pocet="0";}
                    	           int volba=0;
                    	           Object Objekt =map.get(domena);
                    	           int howBig = ((Integer) Objekt).intValue();
                    	           howBig+=Integer.valueOf(pocet).intValue();
                    	       try {
                    	         volba=Integer.valueOf(domena).intValue();
                    	       }
                    	       catch (NumberFormatException e3) {
                    		map.put(domena,new Integer(howBig));
                    	       }
                    				
                    	}
                    	else {
                    	      int volba=0; 
                    	      if (pocet.equals("-")) {
                    	           pocet="0";
                    	      }
                    	      try {
                    	           volba=Integer.valueOf(domena).intValue();
                    	      }
                    	      catch (NumberFormatException e3) {
                    		map.put(domena,new Integer(pocet));
                    	      }
                    	}
                          }
                          return map;
                   }
                   catch (FileNotFoundException  e1) {
                      System.out.println("Dany Subor "+NazevSouboru+" nenalezen");
                      System.exit(1);
                   }
                   catch (IOException e2) {
                   	System.out.println("Nastala chyba pri citani");
                   	System.exit(2);
                   }
                   catch (NumberFormatException e3) {
                   	System.out.println("nastala niekde chyba");
                   }
                   
                   return null;
              }
                
                
	public static void ukoncit() {
		
		/* Class  */

            System.out.println("Na ukoncenie stlacte tlacitko z klavesnice");
            try {
                InputStreamReader klavesa = new InputStreamReader(System.in);
                klavesa.read();
		}
            catch (IOException e) {
	System.out.println("Nastala chyba pri zadani znaku z klavesnice.");
	System.exit(1);
            }	
      }                   
                    		
       public static void VypisMapy (TreeMap map)	{
		
	/* Vypis z mapy */
		
	System.out.println("Domena a dany pocet domen");
		
	Iterator iter =map.entrySet().iterator();
		
	while (iter.hasNext()) {
		Map.Entry dat = (Map.Entry)iter.next();
		String key = (String) dat.getKey(); 
		System.out.println(key+"\t\t"+dat.getValue());
	}
		
	 System.out.println("Domena a dany pocet domen");
		
	ukoncit();
			
}			
		
	 
	
	public static void main (String [] args) {
	
		TreeMap point;
		
		GuiParser apl=new GuiParser("Zpracovaní ftp-logu");
			apl.setLocation(100,100);
			apl.setSize(500,400);
			apl.setVisible(true);
		
	
			point=procesDomen(args[0]);
			VypisMapy(point);
	}
}
```

ishte shkruar 2 here brenda *main* rreshti :


```
 point = procesDomen(args[0]);
```

----------


## mad

ah eshte e shenuar edhe nje shtese ne nje nga rreshtat e pare per rradhitjen e rezultateve edhe ne applet!

----------


## mad

ah, edhe dicka!
si ka mundesi qe megjithese kam futur metoden per rradhitjen e emrave sipas alfabetit. me ben dallim midis shkronjave kapitale, dmth, numeron vec domainet .au dhe vec ato  .AU, qe si praktikisht edhe teorikisht, jane te njejta, apo jo ??!!
{^_^}

----------

