在Java中读取.CSV文件
我正在尝试从.CSV文件读取数据。我已经编写了以下代码,我需要读取3列数据。其中一个将被视为布尔值。以下是我在CSV中的数据 21.1错误 53 12对 50 21对 10错 因此,基于最后一列中的布尔值,尝试在if循环中应用不同的操作。我是java环境的新手,有人能帮我实现吗在Java中读取.CSV文件,java,Java,我正在尝试从.CSV文件读取数据。我已经编写了以下代码,我需要读取3列数据。其中一个将被视为布尔值。以下是我在CSV中的数据 21.1错误 53 12对 50 21对 10错 因此,基于最后一列中的布尔值,尝试在if循环中应用不同的操作。我是java环境的新手,有人能帮我实现吗 while ((line = br.readLine()) != null) { // use comma as separator St
while ((line = br.readLine()) != null) {
// use comma as separator
String[] separator = line.split(cvsSplitBy);
totalPapers = Integer.parseInt(separator[0]);
totalColors = Integer.parseInt(separator[1]);
if (total_papers[2] == "true"){
isDoublePrints = true;
}
isDoublePrints = Boolean.parseBoolean(total_papers[2]);
System.out.println(isDoublePrints);
if(isDoublePrints == true){
totalPapers = (int) (totalBW * 0.12);
totalColors = (int) (totalColors * 0.13);
}
else{
totalPapers = (int) (totalBW * 0.24);
totalColors = (int) (totalColors * 0.22);
}
}
} catch (IOException e) {
e.printStackTrace();
}
每当我运行这个程序时,编译器都会把代码转到else。即使在我的CSV的最后一列中有真实值
[urlRequest setHTTPBody:[noteDataString dataUsingEncoding:NSUTF8StringEncoding]];
此方法可以设置要发送到服务器的数据。您的服务器收到一个字符串“http://domain/Customer_logon.php? email=XXXX&password=XXXX“
,字符串不是有效的json,您的php无法解析它
因此,如果需要json,可以这样编写:
NSString *email = cusemail.text;
NSString *pass = cuspassword.text;
NSDictionary *dic = @{@"email":email,@"password":pass};
[urlRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:dic options:0 error:nil]];
$email=isset($_GET['email'])
这意味着您通过GET
接收参数,而objc代码通过POST
发送参数您最好了解GET和POST first之间的区别。如果您在php中使用$\u GET来接收param,则应在objc中这样编写:
NSString *email = cusemail.text;
NSString *pass = cuspassword.text;
NSString *urlString = [NSString stringWithFormat:@"http://domain/Customer_logon.php?email=%@&password=%@",email,pass];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:urlString];
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *dataRaw, NSURLResponse *header, NSError *error) {
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:dataRaw
options:kNilOptions error:&error];
NSString *status = json[@"status"];
if([status isEqual:@"1"]){
//Success
} else {
//Error
}
}];```
仍然得到相同的错误,我认为PHP有问题。因为它显示在错误日志中PHP通知:未定义索引:PHP通知:未定义索引:emailPHP通知:未定义索引:密码也很抱歉兄弟我不在办公桌上。在同一篇文章中添加了php代码。请看一看,我有一个新的答案