Java 扫描Int时遇到问题
我是Java新手,在过去从未遇到过麻烦的事情上挣扎。不管出于什么原因,我不能扫描代码中的int(或double),但我可以扫描字符串。我正在发布扫描程序无法正常工作的代码片段,请告诉我是否应该包含程序的其余部分Java 扫描Int时遇到问题,java,java.util.scanner,Java,Java.util.scanner,我是Java新手,在过去从未遇到过麻烦的事情上挣扎。不管出于什么原因,我不能扫描代码中的int(或double),但我可以扫描字符串。我正在发布扫描程序无法正常工作的代码片段,请告诉我是否应该包含程序的其余部分 import java.util.Scanner; import java.io.File; import java.io.PrintWriter; import java.io.IOException; public class DZP3 { public static vo
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
public class DZP3
{
public static void main(String[] args) throws IOException
{
announce();
Scanner scan = new Scanner(System.in);
//Prompt user for input file
System.out.println("Greetings! Please enter the filename of the plaintext olympics data file you'd like to open.");
String txtFilename = scan.nextLine();
//Opens olympics data txt file specified, exits if it does not exist
File medalsInput = new File (txtFilename);
if(!medalsInput.exists())
{
System.out.println("File not found. Reload and try again.");
System.exit(1);
}
//Prompt user for output file
System.out.println("Thanks. Please enter the filename of the plaintext data output file.");
String outputTxt = scan.nextLine();
//Create output file specified
File medalsOutput = new File (outputTxt);
//Prompt user for medal cutoff X value
System.out.println("Thanks. Please enter the minimum number of medals a nation must have earned to be counted for calculation 2 listed above. \nEnter the value, as an integer:");
int medalsCutoff = 0;
medalsCutoff = scan.nextInt();
fileProcessing(medalsInput, medalsOutput, medalsCutoff);
}
}
在底部附近,medalsCutoff
不接受任何扫描值。我试着把它放在一个不同于main
的方法中,我试着重新排列它,为它创建一个单独的扫描仪,以及其他一些事情。调试器显示,不管怎样,我都会被卡在那行代码上。我做错了什么?我不知所措
编辑:下面是文件处理方法,以及后面的内容。annound方法就是system.out.println
public static void fileProcessing(File medalsIn, File medalsOut, int medalsMin) throws IOException
{
//Initialize necessary variables and strings
int maxTotMedals = -1;
int natCountMedalsMin = 0;
int natHiScore = -1;
String natName;
String answerOne = "DEFAULT";
int answerTwo = 0;
String answerFour = "DEFAULT";
//Create Printwriter
PrintWriter pw = new PrintWriter(medalsOut);
//Create scanner to read from file, loop until end of file
Scanner filescan = new Scanner(medalsIn);
while (filescan.hasNext())
{
//Initializes medal counting variables at zero, resetting the values with each line
int gCount = 0;
int sCount = 0;
int bCount = 0;
natName = filescan.next();
int lineMedals = 0;
while (lineMedals < 4); //Runs 4 times to cover all four years
{
gCount += filescan.nextInt();
sCount += filescan.nextInt();
bCount += filescan.nextInt();
lineMedals++;
}
int totalMedals = gCount + sCount + bCount;
//Sees if this line's medals have exceeded previous total medal record, if yes, sets country name as answer to question one
if (totalMedals > maxTotMedals)
{
answerOne = natName;
maxTotMedals = totalMedals;
}
if (totalMedals >= medalsMin)
{
natCountMedalsMin++; //For answer two
}
//Score calculation
int natScore = gCount*3;
natScore += sCount*2;
natScore += bCount;
//Compares score to highest score, for answer four
if (natScore > natHiScore)
{
answerFour = natName;
natHiScore = natScore;
}
//Write nation name and score to file
pw.println(natName + " " + natScore);
}
//Define answer two after all countries have been counted
answerTwo = natCountMedalsMin;
//Close output file
pw.close();
//Send results to answer method
answerPrint(answerOne, answerTwo, answerFour, medalsMin, natHiScore);
}
//This method outputs the answers to the user.
public static void answerPrint(String answerEin, int answerZwei, String answerVier, int medalsMini, int HiScore)
{
System.out.println("File read successfully.");
System.out.println("The nation that earned the greatest number of medals is " + answerEin + ".");
System.out.println(answerZwei + " countries earned more than " + medalsMini + " medals.");
System.out.println("The nation with the highest score is " + answerVier + " with a score of " + HiScore + ".");
System.out.println("Thank you for using this program. Until next time!");
}
publicstaticvoidfileprocessing(File-medalsIn、File-medalsOut、int-medalsMin)抛出IOException
{
//初始化必要的变量和字符串
int max=-1;
int-natCountMedalsMin=0;
int-natHiScore=-1;
字符串名称;
字符串answerOne=“默认”;
int-answerTwo=0;
字符串answerFour=“默认”;
//创建Printwriter
PrintWriter pw=新的PrintWriter(medalsOut);
//创建从文件读取的扫描仪,循环直到文件结束
扫描仪文件扫描=新扫描仪(medalsIn);
while(filescan.hasNext())
{
//将奖牌计数变量初始化为零,用每行重置值
int gCount=0;
int-scont=0;
int bCount=0;
natName=filescan.next();
int=0;
while(line奖牌<4);//运行4次以覆盖所有四年
{
gCount+=filescan.nextInt();
scont+=filescan.nextInt();
bCount+=filescan.nextInt();
lined++;
}
奖牌总数=总计数+侦察员+计数;
//查看此行的奖牌是否超过了以前的奖牌总数记录,如果是,则将国家名称设置为问题1的答案
如果(奖牌总数>奖牌总数)
{
answerOne=natName;
最大总奖牌数=总奖牌数;
}
如果(奖牌总数>=medalsMin)
{
natCountMedalsMin++;//答案二
}
//分数计算
int natScore=gCount*3;
natScore+=Scont*2;
natScore+=b计数;
//将答案4的分数与最高分数进行比较
如果(natScore>natHiScore)
{
answerFour=natName;
natHiScore=natScore;
}
//将国家名称和分数写入文件
pw.println(natName+“”+natScore);
}
//在计算完所有国家后,确定答案二
回答2=natCountMedalsMin;
//关闭输出文件
关闭();
//将结果发送到应答方法
answerPrint(answerOne、answerTwo、answerFour、medalsMin、natHiScore);
}
//此方法将答案输出给用户。
公共静态无效应答打印(字符串应答EIN、int应答ZWEI、字符串应答器、int medalsMini、int HiScore)
{
System.out.println(“文件读取成功”);
获得最多奖牌的国家是“+answerEin+”;
System.out.println(answerZwei+“国家获得的奖牌超过”+medalsMini+“奖牌”);
System.out.println(“得分最高的国家是“+answerVier+”,得分为“+HiScore+”);
System.out.println(“感谢您使用这个程序。下次再说吧!”);
}
编辑2:这个问题已经解决了,我的文件处理方法中有一个分号,导致了一个无限循环。谢谢大家的帮助。while(lineamenses<4);
while (lineMedals < 4);
上面的一行末尾有一个分号。这是一个无限循环。创建文件后,使用以下方法 文件medalsOutput=新文件(OUTPUTXT); medalsOutput.createNewFile()文件
在ur代码文件中,未创建并通过syste.exit(1)退出。您是否检查该行System.exit(1);没有执行??被卡在那行代码上,你的意思是程序没有终止吗?对不起,我没有说得更具体。System.exit(1)仅在找不到文件时执行。它似乎工作正常。程序没有终止,它像柠檬一样挂在
medalsCutoff=scan.nextInt()行上代码>直到我手动终止它。嗯,我已经尝试过了,它在我的计算机上运行良好。。也许其他部分有问题..你能提供更多关于文件处理的细节吗?我想问题可能在那里。金牌是给^你明白了。这是一个相当令人尴尬的错误,但如果没有你的帮助,我怀疑我会抓住它。谢谢@AbhinavRanjan。我很惊讶没有更多的错误在里面。@user3418631我想你自己也能找到。有些人只需查看代码就可以看到这一点。我使用调试器检查每一行并确定导致问题的那一行。