【问题标题】:Find max value in java from file input从文件输入中查找java中的最大值
【发布时间】:2012-10-04 19:27:47
【问题描述】:

我是 Java 新手,我正在尝试编写一个程序,要求用户输入仅包含数字的 txt 文件的名称,程序将输出数字的总和、平均值、最大值和最小值在文件中。我已经编写了大部分程序,但是我一直试图找到值的最大值和最小值。您可以提供的任何信息都会有所帮助,如果我不够清楚,我可以尝试详细说明。到目前为止,我的代码是:

public class NumberFile{
    public static void main(String[] args){

      boolean goodName = false;
      int currentNumber, sum = 0, numberCount=0;
      Scanner numberFile = null;
      FileReader infile; 

      Scanner input = new Scanner(System.in);
      System.out.println("Please enter the name of the file you wish to import: ");
      String fileName = input.nextLine();



      while (!goodName){
        try{
          infile = new FileReader(fileName);
          numberFile = new Scanner(infile);
          goodName = true;
        }
        catch (IOException e){
          System.out.println("invalid file name, please enter another name");
          fileName = input.nextLine();
        }
      }
      while (numberFile.hasNextInt()){
        currentNumber = numberFile.nextInt();
        sum+=currentNumber;
        numberCount++;
      }
      System.out.println("The sum of the numbers is " +sum);

      System.out.println("The average of the numbers is " + ((double) sum)/numberCount);



    } // end main
} // end class

【问题讨论】:

    标签: java max min


    【解决方案1】:

    有两个变量min和max(当然min和max最初应该是int.max) 提示:

    if(currentNumber < min)
    {
      min= currentNumber;
    }
    
    if(currentNumber > max)
    {
     max = currentNumber 
    }
    

    以上内容将在您的文件读取循环中。

    【讨论】:

      【解决方案2】:

      声明两个 int 变量 - 一个“min”和一个“max”。 用 Integer.MAX_VALUE 初始化 min,用 Integer.MIN_VALUE 初始化 max。

      然后在您的 while 循环中根据这些变量检查每个数字 - 即。如果数字小于“min”,则将其分配为新的“min”值。 如果它大于“max”,则将其分配为新的“max”值。

      希望这能澄清一下,它非常简单,所以我没有放任何代码,所以你可以自己实现它。

      【讨论】:

      • min 变量不应初始化为 0,因为没有正数会小于该值。相反,将 min 设置为您知道高于输入中任何值的值,例如 Integer.MAX_VALUE。此外,如果您确定输入中没有负数,则应仅将 max 初始化为 0,否则请使用 Integer.MIN_VALUE。
      • Yoy 是绝对正确的,不知道我的头在哪里。谢谢
      • 这工作完美,完全符合我的要求,感谢您没有直接给出代码,我现在会记住它,因为我必须思考。
      【解决方案3】:
      int min=Integer.MAX_VALUE;
      int max=Integer.MIN_VALUE;
      while (numberFile.hasNextInt()){
         currentNumber = numberFile.nextInt();
         sum+=currentNumber;
         numberCount++;
      
         if(min>currentNumber){
            min=currentNumber;
          }
         if(max<currentNumber){
            max=currentNumber;
          }
      }
      

      每次读取小于当前最小值的新值时,将最小值声明为最大 int 值并重新分配最小值。 最大值也是如此。

      【讨论】:

      • 这会起作用,但是它需要是 MAX.VALUE 而不是 Max.Value,您的示例会引发编译错误,但我很感谢您的帮助。
      • 它不应该抛出任何异常/错误。错误信息是什么?而且我没有使用任何 MAX.VALUE ,而是在两者之间使用下划线
      【解决方案4】:

      我有一个实例,我需要从包含大量插入语句的 .sql 文件中找到最大的 Id。这些语句还插入了 Id 以及所有其他字段。我认为这将是一个很好的例子,因为该文件不仅包含整数,而且包含大量混合数据类型。下面是我的程序,

      import java.io.DataInputStream;
      import java.io.FileInputStream;
      import java.io.IOException;
      import java.util.Scanner;
      
      /**
       * @author Hamzeen. H.
       * @date 15/10/2015
       */
      public class Driver {
      
          public Driver() {
          }
      
          private void readData() {
              try {
                  Scanner inputFile = new Scanner(new DataInputStream(
                          new FileInputStream("all_inserts.sql")));
                  long highest = 0L;
      
                  while (inputFile.hasNext()) {
                      String strNum = buildNumber(inputFile.next());
      
                      if (!strNum.equals("")) {
                          Long temp = Long.parseLong(strNum);
                          if (temp > highest) {
                              highest = temp;
                          }
                      }
                  }
                  System.out.println("Highest:: " + highest);
                  inputFile.close();
              } catch (IOException e) {
                  System.out.println("Problem finding file");
              }
          }
      
          private String buildNumber(String str) {
              StringBuilder strBuilder = new StringBuilder();
      
              for (int i = 0; i < str.length(); i++) {
                  char ch = str.charAt(i);
                  if (Character.isDigit(ch)) {
                      strBuilder.append(ch);
                  } else if ('.' == ch || !Character.isDigit(ch) || ',' == ch) {
                      return strBuilder.toString().trim();
                  }
              }
              return strBuilder.toString().trim();
          }
      
          public static void main(String args[]) {
              new Driver().readData();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        • 2016-07-24
        • 1970-01-01
        • 2016-06-07
        • 1970-01-01
        • 1970-01-01
        • 2015-06-29
        相关资源
        最近更新 更多