So here is what I'm trying to do:

I have a file with a list of ip addresses eg.(123.45.6.789:80)

I have two arrays; ip and port, what I want to do is break the address and place the parts into the arrays. so 123.45.6.789 would go into the ip array and 80 would go into the port array.

I want to omit the ":" completely.

Any help would be appreciated. Thanks.

Recommended Answers

All 13 Replies

Have a look at the split method for Strings. You can use it to split your string into an array of 2 substrings by splitting at the :

well yes, I know that. I was rather looking for someone to give me an example of how I could do this.

Member Avatar for hfx642

Where is that little laughing icon???

Ha! I started a couple of sarcastic relies, but thought better of it and didn't post them.

well yes, I know that. I was rather looking for someone to give me an example of how I could do this.

You're asking for something thats very basic in Java

thanks for the help..

Member Avatar for hfx642

You don't know how much I've held back on a LOT of these posts.

jesus christ.

int linenumber = 0;
	   String[] ip = new String[0];
	   String[] port = new String[0];
	   try{
		   
   		File file =new File("c:\\proxylist.txt");

   		if(file.exists()){

   		    FileReader fr = new FileReader(file);
   		    LineNumberReader lnr = new LineNumberReader(fr);

   		    

   	            while (lnr.readLine() != null){
   	        	linenumber++;
   	            }

   	            //put ip's in one array
   	            ip = expand(ip,linenumber);
   	            for (int i=0; i<linenumber; i++)
   	            	ip[i]=
   	            //put ports in one array
   	            port = expand(port,linenumber);
   	            
   	            System.out.println("Total number of proxies in list : " + linenumber);

   	          //  lnr.close();


   		}else{
   			 System.out.println("File does not exists!");
   		}

   	}catch(IOException e){
   		e.printStackTrace();
   	}
Member Avatar for hfx642

You're asking for help with THAT attitude?

Search your input string for the ":" character.
SubString before and after.
Simple stuff.

Right. Now there a few things you need to fix. Here are some to start with.

Your String arrays are size 0. That means you won't be able to store anything in them. Later in the code you get the number of lines in the file, so you could use that to create arrays of the right size at that point.

You use a LineNumberReader, but you don't use the line numbers, so an ordinary BufferedReader will do instead.

You need to read each line into a String so you can parse its contents.

The way you call expand doesn't make much sense - you pass an (empty) array and a number, then assign the result back to the array. You have the same call for ip and port, but there's no way inside expand to know which is which and therefore what to do. And expand has no access to the actual input data to parse it anyway.

There's no attempt to parse the data, using either split or search'n'substring.

You never print the ip and port arrays, so there's no way to see if it worked properly.

ps: Or is expand supposed to re-size the array? That may make more sense. Since you provided neither code nor comments for it I just had to guess.

Thanks, yes I'm using expand to re-size the array. I knew printing the arrays wouldn't do much for me because I still hadn't implemented the split. In other words, nothing was going into the arrays. I'm using the line numbers to determine the size of the array.

Thanks again.

No point in resizing, just leave the initialisation until you know the size

String[] a; // no point initialising here, we don't know the size
...
int lines = (some process that returns the number of lines to be split);
a = new String[lines]; // now we can create an array of the right size
...
int lineNo = 0;
loop thru file reading one line at a time {
   a[lineNo] = (something extracted from the line we just read);
   lineNo++;
}
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.