hi,

i'm wanting to do a for loop on the last line

System.out.println(host + "\t" + status);

of this bit of code so that when i replace

String host = "server";

with something like this:

String hostarray[] = {"pc","server","pc2","another","etc"};

that i get output that looks like this:
Server Up?
pc true
server false
pc2 true
another true
etc true

and this is where i am... and no, i'm no programmer...

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

class isup {	
	public static void main (String args[]) throws 
	 UnknownHostException, IOException{
		String host = "server"; // pc or server name
			int timeOut = 3000; // milliseconds
			boolean status = InetAddress.getByName(host).isReachable(timeOut); // status is true or false
			System.out.println("Server\t\tUp?"); // table heading
			System.out.println(host + "\t" + status); // list of servers and up or not - the tab thing needs some work...
	}
}

thanks in advance... :)

Recommended Answers

All 4 Replies

You can take two arrays, one of the host and another for the servers and then iterate over the arrays.

// list of servers and up or not - the tab thing needs some work...

The comment says it all.
Where are the arrays with the servers and the Up/down settings?
You show one named: hostarray, now you need one for the status.

Read up on how to use a for() loop
and how to use an index to get an element out of an array.
Put the two together using the for() loop's iterating int value as the index to the array.

there need be no array for up or down as
boolean status = InetAddress.getByName(host).isReachable(timeOut);
returns true or false (stored in status)
and that's all i need...

but like you say, looks like i have a lot of reading / figuring out to do...
was just hoping someone had a quick resolve...

Have you used Search on the forum for samples of 'for' statements?
There must be hundreds. Try looking for "for(int"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.