Natrocks
Natrocks

Reputation: 114

Trouble inputting data

I have written a program that is supposed to print the inputs in a certain way. Right now I am using a comma to split the input. However, the list of inputs is given to me in this form:

10.10.4.0-24
10.100.0.0-16
10.102.0.0-16
10.106.0.0-20
10.117.0.0-16
10.118.0.0-16
10.15.128.0-24

And the list is also much longer. I've just been inserting commas between them and putting them on the same line but this is quite tedious. I tried usingString rawdata[]=raw.split(" "); instead of String rawdata[]=raw.split(","); but it does not work in the way I meant it to. Instead of printing the desired output for each input it prints like this:

 Enter ip addresses separated by commas, no spaces(e.g. 10.105.128.0-19,10.120.192.0-22).
10.10.4.0-24
10.100.0.0-16
10.102.0.0-16
10.106.0.0-20
10.117.0.0-16
10.118.0.0-16

set network virtual-router VR-TZ7 routing-table ip static-route 10.118.0.0-16-TSA_TZ nexthop ip-address 10.22.238.99
set network virtual-router VR-TZ7 routing-table ip static-route 10.118.0.0-16-TSA_TZ interface ethernet1/7
set network virtual-router VR-TZ7 routing-table ip static-route 10.118.0.0-16-TSA_TZ metric 10
set network virtual-router VR-TZ7 routing-table ip static-route 10.118.0.0-16-TSA_TZ admin-dist 10
set network virtual-router VR-TZ7 routing-table ip static-route 10.118.0.0-16-TSA_TZ destination 10.118.0.0/16

Is there a separator I can use in my program that will work with numbers in this format?

My program is as follows:

import java.util.Scanner;
public class POPprogTSA
{  
public static void main(String[] args)
{

Scanner s = new Scanner(System.in);
System.out.println("Enter ip addresses separated by commas, no spaces(e.g. 10.105.128.0-19,10.120.192.0-22).");
String raw= s.nextLine();
String rawdata[]=raw.split(",");
/*set network virtual-router VR-TZ7 routing-table ip static-route 10.10.4.0-24-TSA_TZ nexthop ip-address 10.22.238.99
set network virtual-router VR-TZ7 routing-table ip static-route 10.10.4.0-24-TSA_TZ interface ethernet1/7
set network virtual-router VR-TZ7 routing-table ip static-route 10.10.4.0-24-TSA_TZ metric 10
set network virtual-router VR-TZ7 routing-table ip static-route 10.10.4.0-24-TSA_TZ admin-dist 10
set network virtual-router VR-TZ7 routing-table ip static-route 10.10.4.0-24-TSA_TZ destination 10.10.4.0/24
*/  
//TSA
for (int i = 0; i<= rawdata.length-1; i++)
{ 

 System.out.println("set network virtual-router VR-TZ7 routing-table ip static-route " + rawdata[i] + "-TSA_TZ nexthop ip-address 10.22.238.99");
 System.out.println("set network virtual-router VR-TZ7 routing-table ip static-route " + rawdata[i] + "-TSA_TZ interface ethernet1/7");
 System.out.println("set network virtual-router VR-TZ7 routing-table ip static-route " + rawdata[i] + "-TSA_TZ metric 10");
 System.out.println("set network virtual-router VR-TZ7 routing-table ip static-route " + rawdata[i] + "-TSA_TZ admin-dist 10");
 System.out.println("set network virtual-router VR-TZ7 routing-table ip static-route " + rawdata[i] + "-TSA_TZ destination " +(rawdata[i].replace("-","/")));
 System.out.println(" ");





}     
}


}

The result is supposed to be in this form for each number:

set network virtual-router VR-TZ7 routing-table ip static-route 10.10.4.0-24-TSA_TZ nexthop ip-address 10.22.238.99
set network virtual-router VR-TZ7 routing-table ip static-route 10.10.4.0-24-TSA_TZ interface ethernet1/7
set network virtual-router VR-TZ7 routing-table ip static-route 10.10.4.0-24-TSA_TZ metric 10
set network virtual-router VR-TZ7 routing-table ip static-route 10.10.4.0-24-TSA_TZ admin-dist 10
set network virtual-router VR-TZ7 routing-table ip static-route 10.10.4.0-24-TSA_TZ destination 10.10.4.0/24

Any advice would be appreciated!

Upvotes: 0

Views: 47

Answers (1)

Jason C
Jason C

Reputation: 40335

One option is to let the user enter one input at a time, but continuously prompt them (using nextLine() to read) until, say, the word "end" is entered. Then you can copy a line-separated list and paste it into your console; typing "end" at the end.

Alternatively, if you want the user to be able to just copy and paste a bunch into one line, you can use .split("[\\s]+") to allow any amount of whitespace between entries (includes newlines; although nextLine() will eat the newlines anyways).

Another option is to store the list in a text file. Then you can prompt the user for the name of the file to import. You can then open the file and read it line by line.

Upvotes: 2

Related Questions