使用扫描仪读取.csv文件 [英] Reading a .csv file using scanner

查看:67
本文介绍了使用扫描仪读取.csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个涉及向用户询问其邮政编码的项目.使用提供的邮政编码,程序应循环遍历.csv文件以确定他们居住的城市.我可以读取.csv文件中的信息,但是我不知道如何循环遍历以查找特定的城市.信息.

I am working on a project that involves asking a user for their zip code. Using the zip code provided the program should loop through a .csv file to determine what city they live in. I can read the information in the .csv file but I have no idea how to loop through it to find a specific piece of information.

import java.util.Scanner;
import java.io.*;

public class DetermineCity {

public static void main(String[] args) throws IOException {

    String zip = "99820,AK,ANGOON";

    Scanner keyboard = new Scanner(System.in);

    System.out.println("enter then name of a file");
    String filename = keyboard.nextLine();

    File file = new File(filename);
    Scanner inputFile = new Scanner(file);
    String line = inputFile.nextLine();

    System.out.println("The first line in the file is ");
    System.out.println(line);
    inputFile.close();
  }
}

推荐答案

使用Scanner.hasNext()方法循环

Use Scanner.hasNext() method to loop

String Details="";
int ZipCodeIndex=0;
String ZipCode = "10230"
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext()){
     String x=inputFile.nextLine();
     String[] arr=x.split(",");
     if(ZipCode.equals(arr[ZipCodeIndex]))
     {
       Details=x;
       break;
     }
}

这篇关于使用扫描仪读取.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆