Java 扫描仪对象和while循环出现重复局部变量错误

Java 扫描仪对象和while循环出现重复局部变量错误,java,while-loop,java.util.scanner,Java,While Loop,Java.util.scanner,在java中传入Scanner对象并使用while循环访问每一行时,会出现一个错误,即我有一个重复的局部变量(“第一行中的fileContents”) 静态映射计数类别(扫描程序文件内容){ HashMap categoryCount=新HashMap(); while(fileContents.hasNext()){ String line=fileContents.nextLine(); String[]lineItems=line.split(“,”); String category=l

在java中传入Scanner对象并使用while循环访问每一行时,会出现一个错误,即我有一个重复的局部变量(“第一行中的fileContents”)

静态映射计数类别(扫描程序文件内容){
HashMap categoryCount=新HashMap();
while(fileContents.hasNext()){
String line=fileContents.nextLine();
String[]lineItems=line.split(“,”);
String category=lineItems[2];//CSV文件中的指定位置
if(categoryCount.get(category)==null){
分类计数(第1类);
}否则{
categoryCount.put(category,categoryCount.get(category)+1);
}
}
为了进一步说明这一点,我只是从一个文件中组织信息,而且我对Java还不熟悉。我是否正确地使用了这个HashMap,或者我应该以完全不同的方式格式化这个方法和/或内部创建的HashMap

应要求,我的主要任务包括:

    public class PA2Main {
public static void main(String[] args) {
    try {
        String fileName = args[0];
        Scanner fileContents = new Scanner(new File(fileName)); // temp var

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    // recreate fileContents to use outside of try/catch
    String fileName = args[0];
    Scanner fileContents = new Scanner(new File(fileName));
    HashMap<String, Integer> organizedCategories = countCategories(fileContents); // call function

    if (args.length > 1) {
        if (!"LOCATION".equals(args[1]) || !"CATCOUNT".equals(args[1])) {
            System.out.println("Invalid Command");
        } else {
            //process commands
            if (args[1].equals("CATCOUNT")) {

            }

            if (args[1].equals("LOCATION")) {
            // organize info in fi
            }
        }

    }
}
公共类PA2Main{
公共静态void main(字符串[]args){
试一试{
字符串文件名=args[0];
Scanner fileContents=新扫描仪(新文件(文件名));//临时变量
}catch(filenotfounde异常){
e、 printStackTrace();
}
//重新创建要在try/catch之外使用的文件内容
字符串文件名=args[0];
扫描仪文件内容=新扫描仪(新文件(文件名));
HashMap organizedCategories=countCategories(fileContents);//调用函数
如果(参数长度>1){
if(!“LOCATION”.equals(args[1])| |!“CATCOUNT”.equals(args[1])){
System.out.println(“无效命令”);
}否则{
//处理命令
如果(args[1].equals(“CATCOUNT”)){
}
if(args[1].equals(“位置”)){
//在fi中组织信息
}
}
}
}
格式化有点奇怪,但我希望这是有意义的。显然,我没有对程序的其余部分做太多,这不是很干净。错误消息只是说明:重复的局部变量fileContents


还有“快速修复”正在重命名它。

在大多数情况下,一个类中不能有两个名称相同的变量,尤其是如果它们的作用域与在同一方法中的两个变量的作用域相同。即使对象的类型不同,编译器也无法区分和选择要使用的变量,因此无法编译

在您提供的代码中,我们可以看到您使用了具有相同名称的已声明的2个扫描程序对象,这将在每次尝试此操作时导致错误

public static void main(String[] args) {
    try {
        String fileName = args[0];
***     Scanner fileContents = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    String fileName = args[0];
*** Scanner fileContents = new Scanner(new File(fileName));
    HashMap<String, Integer> organizedCategories = countCategories(fileContents); 
    if (args.length > 1) {
        if (!"LOCATION".equals(args[1]) || !"CATCOUNT".equals(args[1])) {
            System.out.println("Invalid Command");
        } else {
            if (args[1].equals("CATCOUNT")) {
            }
            if (args[1].equals("LOCATION")) {
            }
        }
    }
publicstaticvoidmain(字符串[]args){
试一试{
字符串文件名=args[0];
***扫描仪文件内容=新扫描仪(新文件(文件名));
}catch(filenotfounde异常){
e、 printStackTrace();
}
字符串文件名=args[0];
***扫描仪文件内容=新扫描仪(新文件(文件名));
HashMap organizedCategories=countCategories(文件内容);
如果(参数长度>1){
if(!“LOCATION”.equals(args[1])| |!“CATCOUNT”.equals(args[1])){
System.out.println(“无效命令”);
}否则{
如果(args[1].equals(“CATCOUNT”)){
}
if(args[1].equals(“位置”)){
}
}
}
一般来说,要修复此错误,您可以重命名其中一个变量,但在您的情况下,我认为没有必要使用两个Scanner对象

try-catch子句应该专门用于实际可能引发异常的代码行,在本例中是初始化扫描程序

在编写代码时考虑到这一点的一种更简洁的方法是

public static void main (String[] args) {
    String fileName = args[0];
    Scanner fileContents;
    try {
        fileContents = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    HashMap<String, Integer> organizedCategories = countCategories(fileContents);
    if (args.length > 1) {
        if (!"LOCATION".equals(args[1]) || !"CATCOUNT".equals(args[1])) {
            System.out.println("Invalid Command");
        } else {
            if (args[1].equals("CATCOUNT")) {
            }
            if (args[1].equals("LOCATION")) {
            }
        }
    }
}
publicstaticvoidmain(字符串[]args){
字符串文件名=args[0];
扫描仪文件内容;
试一试{
fileContents=新扫描仪(新文件(文件名));
}catch(filenotfounde异常){
e、 printStackTrace();
}
HashMap organizedCategories=countCategories(文件内容);
如果(参数长度>1){
if(!“LOCATION”.equals(args[1])| |!“CATCOUNT”.equals(args[1])){
System.out.println(“无效命令”);
}否则{
如果(args[1].equals(“CATCOUNT”)){
}
if(args[1].equals(“位置”)){
}
}
}
}

您是否在类中的其他地方使用变量名fileContents?是的,我有Scanner fileContents=new Scanner(new File(fileName));在我的主要部分,但这是我创建变量的地方,对吗?你能发布类的其余部分以及你得到的确切错误消息吗请分享整个类。你为什么要创建2个
扫描器
s?当第二个扫描器抛出选中的异常时,它怎么不在
try catch
块中?第一个和第二个代码如何乳头相关?问题是,现在它都在try声明之外,这就是为什么我做了两次。我的一些教授以前说过,把一堆东西放在try块中是不好的风格,所以我尝试把它放在外面,只是重新创建整个东西,因为fileContents的范围有限。有没有办法让e try block statement small还是我应该把所有内容都放到try中?添加了更多信息来回答这个问题哦,我的天啊,谢谢!!我没想到我可以在没有设置任何类似设置的情况下声明扫描仪!
public static void main (String[] args) {
    String fileName = args[0];
    Scanner fileContents;
    try {
        fileContents = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    HashMap<String, Integer> organizedCategories = countCategories(fileContents);
    if (args.length > 1) {
        if (!"LOCATION".equals(args[1]) || !"CATCOUNT".equals(args[1])) {
            System.out.println("Invalid Command");
        } else {
            if (args[1].equals("CATCOUNT")) {
            }
            if (args[1].equals("LOCATION")) {
            }
        }
    }
}