无法输入数据

时间:2014-03-15 03:54:39

标签: java

我编写了一个应该以某种方式打印输入的程序。现在我使用逗号来分割输入。但是,输入列表以这种形式提供给我:

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

列表也更长。我只是在他们之间插入逗号并将它们放在同一条线上,但这非常繁琐。我尝试使用String rawdata[]=raw.split(" ");代替String rawdata[]=raw.split(",");,但它不能按照我的意思使用。而不是为每个输入打印所需的输出,它打印如下:

 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

我的程序中是否有可以使用这种格式的数字的分隔符?

我的计划如下:

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(" ");





}     
}


}

对于每个数字,结果应该采用这种形式:

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

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:2)

一个选项是让用户一次输入一个输入,但不断提示它们(使用nextLine()读取),直到输入“结束”一词。然后,您可以复制以行分隔的列表并将其粘贴到控制台中;最后输入“结束”。

或者,如果您希望用户只能将一堆复制并粘贴到一行中,则可以使用.split("[\\s]+")允许任意条目之间的空白量(包括换行符) ;虽然nextLine()无论如何都会吃新行。)

另一种选择是将列表存储在文本文件中。然后,您可以提示用户输入要导入的文件的名称。然后,您可以打开文件并逐行阅读。