我是Java的新手。我编写了一个程序,用以下格式创建4个json对象。
{"hostname":"host1","username":"user1"}
{"hostname":"host2","username":"user2"}
{"hostname":"host3","username":"user3"}
{"hostname":"host4","username":"user4"}
我使用两个输入文件hosts.lst和users.lst并将它们存储在一个arraylist中
$ cat hosts.lst
host1
host2
host3
host4
$ cat users.lst
user1
user2
user3
user4
现在我想要主机< \ n>在" 1到4"的范围内;但用户< \ n>不应超过" 2"。我无法使用嵌套for循环(我最终创建4 * 2 = 8个对象)。如何控制多个计数器并将它们增加到不同的级别。我希望最终输出看起来像:
{"hostname":"host1","username":"user1"}
{"hostname":"host2","username":"user2"}
{"hostname":"host3","username":"user1"}
{"hostname":"host4","username":"user2"}
一旦用户计数器达到2,它应该从1重新启动。
这是我的代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import org.json.simple.JSONObject;
public class JsonGenerator {
private static ArrayList<String> users = new ArrayList<String>();
private static ArrayList<String> hosts = new ArrayList<String>();
public static void arrayListConstructor1(String filename) throws IOException {
BufferedReader br = null;
br = new BufferedReader(new FileReader(filename));
String line = br.readLine();
while (line != null) {
users.add(line);
line = br.readLine();
}
br.close();
}
public static void arrayListConstructor2(String filename) throws IOException {
BufferedReader br = null;
br = new BufferedReader(new FileReader(filename));
String line = br.readLine();
while (line != null) {
hosts.add(line);
line = br.readLine();
}
br.close();
}
public static void main(String[] args) throws IOException {
for (int counter = 0; counter < 4; counter++) {
arrayListConstructor1("e:\\users.lst");
arrayListConstructor2("e:\\hosts.lst");
String hostname = new String(hosts.get(counter));
String username = new String(users.get(counter));
JSONObject obj = new JSONObject();
obj.put("hostname", hostname);
obj.put("username", username);
System.out.print(obj);
System.out.println("\n");
}
}}
我感谢任何帮助。谢谢!
答案 0 :(得分:0)
您可以尝试以下代码..我们将计数器的模数改为2并加1 ..因此,对于增加的计数器值,该值将在1和2之间循环。
String username = new String(users.get((counter % 2) + 1));
另一种方法是将限制传递给从文件读取用户的方法,然后该方法只能读取所需数量的用户。
arrayListConstructor1("e:\\users.lst", 2);
答案 1 :(得分:0)
就像现在一样,你的代码正在4次读取2个文件,你应该在循环之前移动arrayListConstructor1(...)
和arrayListConstructor2(...)
,只读一次。此外,这种方法似乎做同样的事情,你只能有一种方法来读取文件。要限制用户列表的范围,您可以定义第二个计数器,并在每次达到最大范围时将其设置为0
。这是一个例子:
public static ArrayList<String> arrayListConstructor(String filename) throws IOException {
ArrayList<String> list = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(filename));) {
for (String line; (line = br.readLine()) != null) {
list.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
ArrayList<String> hosts = arrayListConstructor("e:\\\\hosts.lst");
ArrayList<String> users = arrayListConstructor("e:\\\\users.lst");
JSONArray arr = new JSONArray();
for (int counter = 0, counter2 = 0; counter < hosts.size() && users.size() >= 2; counter++, counter2++) {
String hostname = hosts.get(counter);
String username = users.get(counter2);
if (counter2 == 1) {
counter2 = 0;
}
JSONObject obj = new JSONObject();
obj.put("hostname", hostname);
obj.put("username", username);
arr.put(obj);
System.out.print(obj);
System.out.println("\n");
}
System.out.print(arr);
}