在java中更改csv文件中的数据时出错
这是我的customer.csv文件:在java中更改csv文件中的数据时出错,java,list,csv,if-statement,Java,List,Csv,If Statement,这是我的customer.csv文件: 1, Ali,1203456789, Normal 2, Siti,134567890, Normal 3, Bob,1568980765, Normal 我想更改输入到Cased的姓名的正常状态,但我的代码似乎有问题。以下是我的代码: public static void main(String[] args) throws IOException{ Scanner input = new Scanner(System.in); Sys
1, Ali,1203456789, Normal
2, Siti,134567890, Normal
3, Bob,1568980765, Normal
我想更改输入到Cased的姓名的正常状态,但我的代码似乎有问题。以下是我的代码:
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the customer you want to flag as Cased:");
String flagCus = input.nextLine();
ArrayList<String> customersFlagged = new ArrayList<String>();
List<String> lines = Files.readAllLines(Paths.get("customer.csv"));
for (int i = 0; i < lines.size(); i++) {
String[] items = lines.get(i).split(",");
if (items[1] == flagCus){
String enterList = items[0] + "," + items[1] + "," + items[2] + "," + "Cased";
customersFlagged.add(enterList);
} else{
String enterList = items[0] + "," + items[1] + "," + items[2] + "," + items[3];
customersFlagged.add(enterList);
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入要标记为Cased:”)的客户;
字符串flagCus=input.nextLine();
ArrayList CustomerFlagged=新建ArrayList();
列表行=Files.readAllLines(path.get(“customer.csv”);
对于(int i=0;i
我认为问题在于if(items[1]==flagCus)行,但我不确定哪里出错了,在执行if语句时,我尝试在flagCus之前添加一个“”,但仍然出错。有人能帮我检查此代码吗?感谢您的关注
编辑:我应该将代码(项[1]==flagCus)更改为(项[1].equals(“+flagCus”)。谢谢你们的帮助。当比较两个对象而不是原始类型时,请使用
.equals()
而不是=
。因此:
items[1].equals(flagCus);
要检查相等字符串,请使用
“String”。改为使用等于(“其他”)
。文件中的字符串在开头有一个空格(您在逗号上拆分)
从输入数据中删除这些数据或调用:
if (items[1].trim().equals(flagCus)){
(正如其他人在回答中指出的,使用.equals
比较字符串
对象
完整代码:
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the customer you want to flag as Cased:");
String flagCus = input.nextLine();
ArrayList<String> customersFlagged = new ArrayList<String>();
List<String> lines = Files.readAllLines(Paths.get("customer.csv"));
for (int i = 0; i < lines.size(); i++) {
String[] items = lines.get(i).split(",");
if (items[1].trim().equals(flagCus)){
String enterList = items[0] + "," + items[1] + "," + items[2] + "," + "Cased";
customersFlagged.add(enterList);
} else{
String enterList = items[0] + "," + items[1] + "," + items[2] + "," + items[3];
customersFlagged.add(enterList);
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入要标记为Cased:”)的客户;
字符串flagCus=input.nextLine();
ArrayList CustomerFlagged=新建ArrayList();
列表行=Files.readAllLines(path.get(“customer.csv”);
对于(int i=0;i
它正在工作,非常感谢。它正在工作,非常感谢。它的可能副本有点可笑,但这里没有人建议使用任何CSV解析器。从技术上讲,他们建议您使用equals
方法检查字符串的相等性,但是对于一个属于最高级的问题,它很容易获得收益ic Java问题。我友好的建议:请使用适当的工具来解决您的问题。(既不是充满记录的ArrayList
,也不是文件。readAllLines
可能是性能杀手,也不是上的索引循环迭代(对于ArrayList)。迭代器()
或for
,甚至是拆分(“,”
在上断开,
包含值)。
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the customer you want to flag as Cased:");
String flagCus = input.nextLine();
ArrayList<String> customersFlagged = new ArrayList<String>();
List<String> lines = Files.readAllLines(Paths.get("customer.csv"));
for (int i = 0; i < lines.size(); i++) {
String[] items = lines.get(i).split(",");
if (items[1].trim().equals(flagCus)){
String enterList = items[0] + "," + items[1] + "," + items[2] + "," + "Cased";
customersFlagged.add(enterList);
} else{
String enterList = items[0] + "," + items[1] + "," + items[2] + "," + items[3];
customersFlagged.add(enterList);
}
}