Java 将方法中的数据与另一个方法中的 hashmap 进行比较
Java compare the data from method to hashmap in another method
此代码正在读取文本文件并创建 HashMap。我想做的是比较我的 ModelOne.txt 和 ModelTwo.txt
中的 StatusMAP 方法
假设我的 ModelOne.txt 有这样的数据:
Model1, 54321, Test, Online
在我的 ModelTwo.txt 上有这样的数据:
Model2, 12345, Check, OFF
我已经知道如何获取文本文件中的索引数据,但我不能做的是基于 StatusMAP 将这两个数据相互比较。所以基本上,如果 ModelOne.txt 与 ModelTwo.txt 比较,它将获得索引 [3],然后它将比较索引 [3] 在 ModelOne 中是否在线,如果不是,则在 ModelTwo 中应该是 OL,它将将打印 'Not equal' 否则 'They're Equal'.
这篇文章读起来确实很长,但如有任何帮助、建议或评论,我们将不胜感激。提前谢谢大家。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.io.*;
public class ReadTxt {
public static void main(String[] args) throws Exception{
ReadTxt obj = new ReadTxt();
obj.PackageMAP();
obj.StatusMAP();
obj.ReadModelOne();
obj.ReadModelTwo();
}
public static void StatusMAP() {
Map<String, String> hashstatus = new HashMap<String, String>();
hashstatus.put("Offline", "OFF");
hashstatus.put("Online", "OL");
}
public static void PackageMAP() {
String csvFile = "Names.txt";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
Map<String, String> maps = new HashMap<String, String>();
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] packagecode = line.split(cvsSplitBy);
maps.put(packagecode[0], packagecode[1]);
}
//loop map
for (Map.Entry<String, String> entry : maps.entrySet()) {
System.out.println("Name Package one \t" + entry.getKey()
+ "\t"
+ "|"
+ "\t"
+ "Name Package two \t"
+ entry.getValue());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("This is all the Name packages");
}
public static void ReadModelOne() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("ModelOne.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] MOne_STATUS = line.split(",");
for (String inputOne : MOne_STATUS) {
inputOne = MOne_STATUS[2];
}
}
br.close();
}
public static void ReadModelTwo() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("ModelTwo.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] MTwo_STATUS = line.split(",");
for (String inputTwo : MTwo_STATUS) {
inputTwo = MTwo_STATUS[2];
}
}
br.close();
}
public static void checkStatus() {
// do the checking here for status
}
}
尝试如下,
String data1 = "Model1, 54321, Test, Online";
String data2 = "Model2, 12345, Check, OFF";
String output = (data1.split(", ")[3].equalsIgnoreCase("Online") &&
data2.split(", ")[3].equalsIgnoreCase("OL")) ?
"Equal" :
"Not Equal";
System.out.println(output);
此代码正在读取文本文件并创建 HashMap。我想做的是比较我的 ModelOne.txt 和 ModelTwo.txt
中的 StatusMAP 方法假设我的 ModelOne.txt 有这样的数据:
Model1, 54321, Test, Online
在我的 ModelTwo.txt 上有这样的数据:
Model2, 12345, Check, OFF
我已经知道如何获取文本文件中的索引数据,但我不能做的是基于 StatusMAP 将这两个数据相互比较。所以基本上,如果 ModelOne.txt 与 ModelTwo.txt 比较,它将获得索引 [3],然后它将比较索引 [3] 在 ModelOne 中是否在线,如果不是,则在 ModelTwo 中应该是 OL,它将将打印 'Not equal' 否则 'They're Equal'.
这篇文章读起来确实很长,但如有任何帮助、建议或评论,我们将不胜感激。提前谢谢大家。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.io.*;
public class ReadTxt {
public static void main(String[] args) throws Exception{
ReadTxt obj = new ReadTxt();
obj.PackageMAP();
obj.StatusMAP();
obj.ReadModelOne();
obj.ReadModelTwo();
}
public static void StatusMAP() {
Map<String, String> hashstatus = new HashMap<String, String>();
hashstatus.put("Offline", "OFF");
hashstatus.put("Online", "OL");
}
public static void PackageMAP() {
String csvFile = "Names.txt";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
Map<String, String> maps = new HashMap<String, String>();
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] packagecode = line.split(cvsSplitBy);
maps.put(packagecode[0], packagecode[1]);
}
//loop map
for (Map.Entry<String, String> entry : maps.entrySet()) {
System.out.println("Name Package one \t" + entry.getKey()
+ "\t"
+ "|"
+ "\t"
+ "Name Package two \t"
+ entry.getValue());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("This is all the Name packages");
}
public static void ReadModelOne() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("ModelOne.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] MOne_STATUS = line.split(",");
for (String inputOne : MOne_STATUS) {
inputOne = MOne_STATUS[2];
}
}
br.close();
}
public static void ReadModelTwo() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("ModelTwo.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] MTwo_STATUS = line.split(",");
for (String inputTwo : MTwo_STATUS) {
inputTwo = MTwo_STATUS[2];
}
}
br.close();
}
public static void checkStatus() {
// do the checking here for status
}
}
尝试如下,
String data1 = "Model1, 54321, Test, Online";
String data2 = "Model2, 12345, Check, OFF";
String output = (data1.split(", ")[3].equalsIgnoreCase("Online") &&
data2.split(", ")[3].equalsIgnoreCase("OL")) ?
"Equal" :
"Not Equal";
System.out.println(output);