当前位置: 首页 > 知识库问答 >
问题:

扫描Int时遇到问题

邵绪
2023-03-14

我是Java新手,在过去从未遇到过麻烦的事情上苦苦挣扎。不管出于什么原因,我不能扫描代码中的int(或double),但我可以扫描字符串。我正在发布扫描程序无法正常工作的代码片段,请告诉我是否应该包含程序的其余部分。

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的方法中,我试着重新排列它,为它创建一个单独的扫描仪,以及其他一些事情。调试器显示,不管怎样,我都会被卡在那行代码上。我做错了什么?我不知所措。

编辑:这是文件处理方法,以及之后的内容。宣布方法只是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!");

}

编辑2:这个问题已经解决了,我的文件处理方法中有一个丢失的分号,导致了无限循环。谢谢大家的帮助。

共有2个答案

葛浩阔
2023-03-14

创建文件后,使用以下方法File medalsOutput=new File(outputTxt); medalsOutput.createNewFile()

在您的代码文件中,未创建并通过syste退出。出口(1)

卫浩瀚
2023-03-14
while (lineMedals < 4);

上面一行末尾有一个分号。这是一个无限循环。

 类似资料:
  • 本文向大家介绍Android设备获取扫码枪扫描的内容与可能遇到的问题解决,包括了Android设备获取扫码枪扫描的内容与可能遇到的问题解决的使用技巧和注意事项,需要的朋友参考一下 前言 大家应该都有所体会,在生活中条形码扫码枪可是随处可见,可以很迅速地扫描出条形码内容,比什么手机相机扫码快了不是一点两点。 为了节约成本,扫码枪可以直接通过蓝牙连接android或其他设备。 那么android设备如

  • 实时扫描可根据 Burp 的子工具中的请求和响应自动执行一些扫描操作。 您可以使用实时扫描来审计漏洞, 或是吧观察到的资源添加到 Burp 的 Target 工具的站点地图(Site map) 实时扫描配置 实时扫描需要配置以下内容才可以进行使用: 工具范围 - 您可以选择要检查其流量的工具,以选择由实时扫描处理的项目。 URL范围 - 您可以根据URL来定义实时扫描处理的项目。 您可以选择包含所

  • 问题内容: 如何将扫描仪的定界符设置为;或换行? 我试过: 但这不起作用。 问题答案: 通常,在模式中,您需要将翻倍。 所以,尝试 要么 编辑 :如果是问题,则可能要尝试以下操作: 它匹配的一个或多个,和。 注意 :我还没有尝试过这些。

  • 问题内容: 我正在尝试读取具有多个单词的字符串,即。洛杉矶或纽约市。如果有两个词并将它们分割在变量之间,则使用scanner.next()进行“出发”和“到达”只会读取第一个。nextLine()也不是很幸运。这是我的代码: 我知道这很简单,但我还没有弄清楚。 这是上面的代码的输入/输出: 输入航班号:29 输入出发城市:(立即跳至下一行) 输入到达城市: -—我真正想要的是---- 输入航班号:

  • 一段时间以来,我一直在做一个作为拼字字典的编程作业。该程序接受用户的输入,并输出一个包含单词列表的文件,这取决于用户从菜单中请求的内容。我遇到的问题与Scanner.nextLine()有关。 我不是很清楚为什么,但由于某种原因,有时我必须按一次enter键,然后我的代码才会接受我的输入并将其存储为变量。本质上,我最终输入了两次输入。我尝试在代码周围插入Scanner.nextLine()以“占用

  • 我对DynamoDB的性能有问题,我想澄清一下我有点困惑的地方。 执行扫描表中的100条记录,条件是使用(例如)。如果在表中找到20条记录,DynamoDB是否还扫描其他80条记录? 扫描时分页是如何工作的? 消耗超过分配的RCU和WCU的后果是什么?