🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.java
4 messages
4 total messages Started by LprzemekL Wed, 12 Oct 2011 19:37
ArrayList return w petli
#152960
Author: LprzemekL
Date: Wed, 12 Oct 2011 19:37
43 lines
2574 bytes
Hej,
Mam taka metode, ktora zwraca wynik ping:

  int numLines = 0;

  public String getPing(String result, int numLines, int arraySize,
String result1, String result2) {
	  String line = null;
	  ArrayList<String> arrayList = new ArrayList<String>();
		try {
			  Process p = Runtime.getRuntime().exec("ping -n 2 " + result);
			  BufferedReader input = new BufferedReader(new
InputStreamReader(p.getInputStream()));

			  while ((line = input.readLine()) != null) {
				  System.out.println(line);	// pomocne tylko podczas analizy
				  arrayList.add(numLines, line);
				  numLines++;
			  }
			  arraySize = arrayList.size();
			  result = arrayList.get(1);
			  result1 = arrayList.get(2);
			  result2 = arrayList.get(3);
			  input.close();

		} catch (Exception err) {
			  err.printStackTrace();
			  return "ErrorList";
		  }
		return result + result1 + result2;
  }


Wynik pozniej bedzie wyrzucony na etykiete Label, komponent GWT.
Pytanie: jak uzyskac taki efekt, zeby wynik wyrzucany byl z automatu
(wiersz po wierszu) a nie tak jak w sytucji powyzszej, gdzie dodaje
manualnie result, result1, result2, ...
W tej chwili, to nie jest dobre rozwiazanie, gdyz recznie musze
wybierac alementy z ArrayList a chcialbym, zeby to bylo robione z
automatu (w jakiejs petli) i wyrzucane (cos z stylu result +
numLines).
Obiekt arraySize bedzie mial rozny rozmiar w zaleznosci od "ping -n X
" + result, gdzie result to jest textBox.getText().
Re: ArrayList return w petli
#152961
Author: Kacper Szymański
Date: Thu, 13 Oct 2011 08:30
45 lines
3659 bytes
On 12 Paź, 21:37, LprzemekL <zubertow...@tenbit.pl> wrote:
> Hej,
> Mam taka metode, ktora zwraca wynik ping:
>
>   int numLines = 0;
>
>   public String getPing(String result, int numLines, int arraySize,
> String result1, String result2) {
>           String line = null;
>           ArrayList<String> arrayList = new ArrayList<String>();
>                 try {
>                           Process p = Runtime.getRuntime().exec("ping -n 2 " + result);
>                           BufferedReader input = new BufferedReader(new
> InputStreamReader(p.getInputStream()));
>
>                           while ((line = input.readLine()) != null) {
>                                   System.out.println(line);     // pomocne tylko podczas analizy
>                                   arrayList.add(numLines, line);
>                                   numLines++;
>                           }
>                           arraySize = arrayList.size();
>                           result = arrayList.get(1);
>                           result1 = arrayList.get(2);
>                           result2 = arrayList.get(3);
>                           input.close();
>
>                 } catch (Exception err) {
>                           err.printStackTrace();
>                           return "ErrorList";
>                   }
>                 return result + result1 + result2;
>   }
>
> Wynik pozniej bedzie wyrzucony na etykiete Label, komponent GWT.
> Pytanie: jak uzyskac taki efekt, zeby wynik wyrzucany byl z automatu
> (wiersz po wierszu) a nie tak jak w sytucji powyzszej, gdzie dodaje
> manualnie result, result1, result2, ...
> W tej chwili, to nie jest dobre rozwiazanie, gdyz recznie musze
> wybierac alementy z ArrayList a chcialbym, zeby to bylo robione z
> automatu (w jakiejs petli) i wyrzucane (cos z stylu result +
> numLines).
> Obiekt arraySize bedzie mial rozny rozmiar w zaleznosci od "ping -n X
> " + result, gdzie result to jest textBox.getText().

Nadpisz metodę toString()
Re: ArrayList return w petli
#152962
Author: Kacper Szymański
Date: Thu, 13 Oct 2011 08:31
45 lines
3743 bytes
On 12 Paź, 21:37, LprzemekL <zubertow...@tenbit.pl> wrote:
> Hej,
> Mam taka metode, ktora zwraca wynik ping:
>
>   int numLines = 0;
>
>   public String getPing(String result, int numLines, int arraySize,
> String result1, String result2) {
>           String line = null;
>           ArrayList<String> arrayList = new ArrayList<String>();
>                 try {
>                           Process p = Runtime.getRuntime().exec("ping -n 2 " + result);
>                           BufferedReader input = new BufferedReader(new
> InputStreamReader(p.getInputStream()));
>
>                           while ((line = input.readLine()) != null) {
>                                   System.out.println(line);     // pomocne tylko podczas analizy
>                                   arrayList.add(numLines, line);
>                                   numLines++;
>                           }
>                           arraySize = arrayList.size();
>                           result = arrayList.get(1);
>                           result1 = arrayList.get(2);
>                           result2 = arrayList.get(3);
>                           input.close();
>
>                 } catch (Exception err) {
>                           err.printStackTrace();
>                           return "ErrorList";
>                   }
>                 return result + result1 + result2;
>   }
>
> Wynik pozniej bedzie wyrzucony na etykiete Label, komponent GWT.
> Pytanie: jak uzyskac taki efekt, zeby wynik wyrzucany byl z automatu
> (wiersz po wierszu) a nie tak jak w sytucji powyzszej, gdzie dodaje
> manualnie result, result1, result2, ...
> W tej chwili, to nie jest dobre rozwiazanie, gdyz recznie musze
> wybierac alementy z ArrayList a chcialbym, zeby to bylo robione z
> automatu (w jakiejs petli) i wyrzucane (cos z stylu result +
> numLines).
> Obiekt arraySize bedzie mial rozny rozmiar w zaleznosci od "ping -n X
> " + result, gdzie result to jest textBox.getText().

Hej, spróbuj nadpisać metodę toString()
Re: ArrayList return w petli
#152963
Author: Marcin Biegan
Date: Thu, 13 Oct 2011 17:15
52 lines
2863 bytes
On 2011-10-12 21:37, LprzemekL wrote:

> Wynik pozniej bedzie wyrzucony na etykiete Label, komponent GWT.
> Pytanie: jak uzyskac taki efekt, zeby wynik wyrzucany byl z automatu
> (wiersz po wierszu) a nie tak jak w sytucji powyzszej, gdzie dodaje
> manualnie result, result1, result2, ...
> W tej chwili, to nie jest dobre rozwiazanie, gdyz recznie musze
> wybierac alementy z ArrayList a chcialbym, zeby to bylo robione z
> automatu (w jakiejs petli) i wyrzucane (cos z stylu result +
> numLines).
> Obiekt arraySize bedzie mial rozny rozmiar w zaleznosci od "ping -n X
> " + result, gdzie result to jest textBox.getText().

1) Parametry tej metody są podejrzane, jakby... zbędne

2) Np. przekaż tej metodzie callback, który będziesz wywoływał po każdej 
iteracji, a którego implementacja dopisuje dany String na koniec kontrolki Label.

interface PingCallback{
   void onNewLine(String line);
   void onError(Exception e);
}

public void ping(String address, PingCallback callback) {
   try {
     Process p = Runtime.getRuntime().exec("ping -n 2 " + address);
     BufferedReader input = new BufferedReader(new
           InputStreamReader(p.getInputStream()));
     String line;
     while ((line = input.readLine()) != null) {
       System.out.println(line); // pomocne tylko podczas analizy
       callback.onNewLine(line);
     }
     input.close();
   } catch (Exception err) {
     callback.onError(err);
   }
}
//...
ping("google.com", new PingCallback(){
   public void onNewLine(String line) {
     label.setText(label.getText() + "," + line);
   }
   public void onError(Exception e) {
     // obsluzyc wyjatek jakos
     label.setText("FAIL! " + e.getMessage());
   }
});

-- 
pozdrawiam
Marcin Biegan
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads