程序正在给我一个“线程异常”main“java.util.inputmismatchexception”

kyvafyod  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(316)

这是我正在为我的程序使用的代码

import java.util.*;

import java.io.*;

import java.util.ArrayList;

/**
 * This program will read the information from the 
 * txt file and will organize and output the information
 * in a clean and understandable manner 
 */
public class Project3_S_B {// Open class container
    public static void main(String[] args) throws FileNotFoundException { // Open main method

        File file = new File("M:\\Rooms2.txt"); //This gets the txt file from my computers file explorer

        Scanner txtFile = new Scanner(file); //This scans the txt file that was pulled from my computer

        String roomName =""; //variable that will hold the value for the name of each room
        String roomShade = ""; //variable that will hold the value for the shade choice chosen
        String acType = ""; //variable that will hold the value for Air Conditioner type
        String acManufacturer = ""; //variable that will hold the value for the Air Conditioner manufacturer
        double roomLength = 0.0; //variable that will hold the value for the rooms length 
        double roomCoolingCapacity = 0.0; //variable that will hold the value for the rooms cooling capacity
        Double acCoolingCapacity = 0.0;
        double roomWidth = 0.0; //variable that will hold the value for the rooms width 

        ArrayList<Room> room = new ArrayList<Room>(); //This is the array list for the room name and its information

        ArrayList<AirConditioner> airconditioner = new ArrayList<AirConditioner>(); //This is the array list for the Air Conditioner and its information

    while(txtFile.hasNext()){ //This while loop will organize the information from the txt file into its categories

        txtFile.next();

        roomName = txtFile.next();

        roomLength = txtFile.nextDouble();

        roomWidth = txtFile.nextDouble();

        roomShade = txtFile.next();

        acManufacturer = txtFile.next();

        acType = txtFile.next();

        acCoolingCapacity = txtFile.nextDouble();

        txtFile.nextLine();

        if(txtFile.hasNext()){ //This will repeat the while loop if there arew more lines than the while loop accounts for

            txtFile.nextLine();

            txtFile.nextLine();

        }

        Room r = new Room(roomName, roomLength, roomWidth, roomShade);

        room.add(r);

        AirConditioner a = new AirConditioner(acManufacturer, acType, roomCoolingCapacity);

        airconditioner.add(a);

    }

    for(int i =0; i < room.size(); i++){ //This for loop will output the information from the txt file
        System.out.println("Room Name: " +room.get(i) .getroomName());

        System.out.println("Room Area(in square feet): " +room.get(i) .getroomArea());

        System.out.println("Amount of Shade: " +room.get(i) .getroomShade());

        System.out.printf("BTU Per hour needed: %,.0f", room.get(i) .calroomCoolingCapacity());

        System.out.println("Air Conditioner Manufacturer: " +airconditioner.get(i) .getacManufacturer());

        System.out.println("Air Conditioner Type: " +airconditioner.get(i) .getacType());

        System.out.printf("Air Conditioner Cooling Capacity (BTUs Per Hour): %,.0f" +airconditioner.get(i) .getacCoolingCapacity());

        System.out.println("" +room.get(i) .hasAdequateCooling());

    }

    } //Close main method
} //Close class container

这就是当我试着运行它时它给我的错误

Exception in thread "main" java.util.InputMismatchException
        at java.base/java.util.Scanner.throwFor(Scanner.java:939)       
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)    
        at Project3_S_B.main(Project3_S_B.java:38)

我查看了所有可能出现的问题,我的所有数据类型都与txt文件中的数据类型匹配。一位朋友建议我尝试异常处理,但我不知道如何将它合并到我的代码中,即使可以,我也被指示不要在代码中使用它。

Master Bathroom
10
15.5
Abundant
GE
Portable
8000

Master Bedroom
16
16.25
Moderate
Frigidaire
Portable
8000

Living Room
30
25
Little
Whirlpool
Window Mounted
18000

Kitchen
20
12
Moderate
GE
Window Mounted
6000
e7arh2l6

e7arh2l61#

编辑:
扫描仪使用计算机的默认区域设置。如果你改变它,它会工作:
txtfile.uselocale(locale.english);
它说异常出现在第38行:
roomname=txtfile.next();
你能出示你的txt文件吗?我从未使用过scanner(),所以我想测试您的代码。

相关问题