gpt4 book ai didi

java - Integer.parseint 异常

转载 作者:行者123 更新时间:2023-12-01 13:17:45 25 4
gpt4 key购买 nike

问题是:编写一个程序来处理 input.txt 文件,该文件包含有关机票类型的数据,后跟所覆盖的里程,并报告该人获得了多少常旅客里程。

  • 乘坐经济舱每行驶一英里即可赚取 1 飞行常客里程。
  • 乘坐头等舱每行驶一英里即可赚取 2 飞行常客里程。
  • 搭乘折扣航类可赚取 0 飞行常客里程。

例如,考虑到下面 input.txt 中的数据,您的方法必须返回 15600 (2*5000 + 1500 + 100 + 2*2000)。

输入.txt:

firstclass 5000 coach 1500 coach
100 firstclass 2000 discount 300

我的代码给我带来了 parseint 方法的问题。任何帮助将不胜感激:)

//InInteger class
import java.lang.NumberFormatException;
public class IsInteger {

public static boolean IsaInteger (String s)throws NumberFormatException
{
try
{
Integer.parseInt(s);//converts the string into an integer
return true;
}
catch (NumberFormatException e)
{
return false;
}
}

}

//main class

import java.io.*;
import java.util.StringTokenizer;


public class LA5ex2 {

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


BufferedReader input= new BufferedReader (new InputStreamReader (new FileInputStream("C:/Users/user/workspace/LA5ex2/input.txt")));
String str;
int TotalMiles=0;
try {
int mileage,lines=0;
String check,copy=null;
String word=null;
boolean isString=false;

while ((str = input.readLine()) != null)
{
lines++;
StringTokenizer token = new StringTokenizer(str);
while (token.hasMoreTokens())
{
if ((lines>1) && (isString))
{
//do nothing
}
else
{word= token.nextToken();
copy=word;}
if (token.hasMoreTokens())
mileage= Integer.parseInt(token.nextToken());
else
{
if (!(IsInteger.IsaInteger(word)))
{
copy=word;
isString=true;
}

break;
}
if (copy.equals("firstclass"))
TotalMiles+= (2*mileage);
else if (copy.equals("coach"))
TotalMiles+= (1*mileage);
else if (copy.equals("discount"))
TotalMiles+= (0*mileage);
}
}


System.out.println("Frequent-flier miles the person earns: "+ TotalMiles);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

最佳答案

这是我在运行您的代码时得到的堆栈跟踪:

Exception in thread "main" java.lang.NumberFormatException: For input string: "firstclass"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:514)
at LA5ex2.main(LA5ex2.java:30)

我认为这是您在评论中提到的错误。但是,NumberFormatException 不会出现在 IsInteger 类中的 IsaInteger() 方法中(您可以通过返回 来 try catch 它) truefalse),但在 LA5ex2 类中(您也可以 try catch 它,但如果它崩溃,则仅打印堆栈跟踪)。当 Integer.parseInt() 尝试将字符串 firstclass 解析为整数时,会发生异常,这当然会失败:

if(token.hasMoreTokens()) mileage = Integer.parseInt(token.nextToken());
<小时/>

我使用您的 IsaInteger< 在 LA5ex2.java 中用两个 ArrayList 重写了您的代码(以跟踪各种飞行器类别和各种里程)/方法: 导入java.io.*; 导入java.util.ArrayList; 导入 java.util.StringTokenizer;

public class LA5ex2 {

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

BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
String str = null;
String token = null;
int totalMiles = 0;
int lines = 0;
ArrayList<String> flierClasses = new ArrayList<String>();
ArrayList<Integer> mileages = new ArrayList<Integer>();

try {
while((str = input.readLine()) != null) {
lines++; // Why are we counting the lines, anyway?
StringTokenizer tokenizer = new StringTokenizer(str);
while(tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();

if(!(IsInteger.IsaInteger(token))) {
flierClasses.add(token); // if it's not an int, we assume it's a flier class
} else {
mileages.add(Integer.parseInt(token)); // if it's an int, it's a mileage
}
}
}
} catch(NumberFormatException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
} catch(IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}

// Add everything up
for(int i = 0; i < flierClasses.size(); i++) {
totalMiles += calculateFlierMiles(flierClasses.get(i), mileages.get(i));
}

System.out.println("Frequent-flier miles the person earns: " + totalMiles);
}

private static int calculateFlierMiles(final String flierClass, final int mileage) {
if(flierClass.equals("firstclass")) return(2 * mileage);
else if(flierClass.equals("coach")) return(1 * mileage);
else if(flierClass.equals("discount")) return(0 * mileage);
return 0;
}
}

此代码为我提供了所需的输出:此人赚取的常旅客里程:15600

关于java - Integer.parseint 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22330471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com