使用分隔符从文件中读取以查找最大和最小,获取整数最大/最小而不是文件中的数字

发布于 01-19 15:30 字数 1671 浏览 3 评论 0原文

我试图从文本文件中找到最大和最小的数字,并查看了 stackowerflow 上的其他示例。

但是当我尝试时,我最终得到的是整数最大值和最小值,而不是文件中的值。 由于文件中的数字应该用分号来分隔,因此我尝试使用分隔符。

import java.io.IOException;
import java.util.Scanner;
import java.io.File;
public class LargestAndSmallest
    {
        public static void main(String[] args) throws IOException
        {
            File file = new File("src/Number.txt");
            if (!file.exists())
            {
                System.out.println("File not found.");
                System.exit(0);
            }

            Scanner sc = new Scanner(new File("src/Number.txt"));

            System.out.println("Writes out numbers from the file so the user can se them");     
            System.out.println(sc.nextLine());              

            sc.useDelimiter(";");

            int max = Integer.MIN_VALUE;                              
            int min = Integer.MAX_VALUE;                              

            while (sc.hasNextInt())                       
            {
                int currentNumber  = sc.nextInt();      

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

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

            sc.close();                                      

            System.out.println("Smallest number: " +min);       
            System.out.println("Largest number: " +max);      
        }
    }

I´m trying to find the largest and smallest number from a textfile and has looked on other examples on stackowerflow.

But when I try I ends upp with Integer max and min value, not values from the file.
Since the numbers in the files are supposed to have semicolon to seperate them, I have tried to use delimiter.

import java.io.IOException;
import java.util.Scanner;
import java.io.File;
public class LargestAndSmallest
    {
        public static void main(String[] args) throws IOException
        {
            File file = new File("src/Number.txt");
            if (!file.exists())
            {
                System.out.println("File not found.");
                System.exit(0);
            }

            Scanner sc = new Scanner(new File("src/Number.txt"));

            System.out.println("Writes out numbers from the file so the user can se them");     
            System.out.println(sc.nextLine());              

            sc.useDelimiter(";");

            int max = Integer.MIN_VALUE;                              
            int min = Integer.MAX_VALUE;                              

            while (sc.hasNextInt())                       
            {
                int currentNumber  = sc.nextInt();      

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

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

            sc.close();                                      

            System.out.println("Smallest number: " +min);       
            System.out.println("Largest number: " +max);      
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦晓ヶ微光ヅ倾城 2025-01-26 15:30:45

试试这个

 import java.io.IOException;
 import java.util.Scanner;
 import java.io.File;
 public class LargestAndSmallest
 { public static void main(String[] args) throws IOException
  {
      File file = new File("src/Number.txt");
      if (!file.exists())
      {
          System.out.println("File not found.");
          System.exit(0);
      }

      Scanner sc = new Scanner(new File("src/Number.txt"));

      sc.useDelimiter(";");
      System.out.println("Writes out numbers from the file so the user see");     
     
     int max = Integer.MIN_VALUE;                              
     int min = Integer.MAX_VALUE;            
             
     
     while (sc.hasNextInt())                       
     {
         

         int currentNumber  = sc.nextInt();      
         System.out.println(currentNumber);
         if ( currentNumber  > max)                               
         {
             max = currentNumber ;                                
         }

         if ( currentNumber  < min)                               
         {
             min = currentNumber ;                                
         }
         
         
     }
     sc.close();
                                        

     System.out.println("Smallest number: " +min);       
     System.out.println("Largest number: " +max);      
 }
}

try this

 import java.io.IOException;
 import java.util.Scanner;
 import java.io.File;
 public class LargestAndSmallest
 { public static void main(String[] args) throws IOException
  {
      File file = new File("src/Number.txt");
      if (!file.exists())
      {
          System.out.println("File not found.");
          System.exit(0);
      }

      Scanner sc = new Scanner(new File("src/Number.txt"));

      sc.useDelimiter(";");
      System.out.println("Writes out numbers from the file so the user see");     
     
     int max = Integer.MIN_VALUE;                              
     int min = Integer.MAX_VALUE;            
             
     
     while (sc.hasNextInt())                       
     {
         

         int currentNumber  = sc.nextInt();      
         System.out.println(currentNumber);
         if ( currentNumber  > max)                               
         {
             max = currentNumber ;                                
         }

         if ( currentNumber  < min)                               
         {
             min = currentNumber ;                                
         }
         
         
     }
     sc.close();
                                        

     System.out.println("Smallest number: " +min);       
     System.out.println("Largest number: " +max);      
 }
}
梦途 2025-01-26 15:30:45

来到您的问题上删除此行system.out.println(sc.nextline());
在这样做的过程中,您是在将光标移动到null的下一行中,因此您的值将在

int max = Integer.MIN_VALUE;                              
int min = Integer.MAX_VALUE; 

您的while liop中不执行,因为sc为null,您已经在执行system时已经将光标转移到null。 out.println(sc.nextline());

访问

尝试此代码以更好地优化; -

时间复杂性 - ; o(n log(n))

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Scanner;

public class LargestAndSmallest{
    public static void main(String[] args) throws IOException, ParseException {

        File file = new File("src/Number.txt");
        if (!file.exists()) {
            System.out.println("File not found.");
            System.exit(0);
        }

        Scanner sc = new Scanner(new File("src/Number.txt"));

        System.out.println("Writes out numbers from the file so the user can se them");

        int[] arr = Arrays.stream(sc.nextLine().trim().split(";")).mapToInt(Integer::parseInt).toArray();
        Arrays.sort(arr);
        int max = arr[arr.length - 1];
        int min = arr[0];


        sc.close();

        System.out.println("Smallest number: " + min);
        System.out.println("Largest number: " + max);
    }

}


Coming to your question remove this line System.out.println(sc.nextLine());
as doing this you are making your cursor move in the next line of the file which is null so your values are getting initialize with

int max = Integer.MIN_VALUE;                              
int min = Integer.MAX_VALUE; 

your while loop is not executing as sc is null you already move your cursor to null while doing System.out.println(sc.nextLine());

visit this for more explanation

Try This Code for better optimization;-

Time Complexity -; O(n log(n))

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Scanner;

public class LargestAndSmallest{
    public static void main(String[] args) throws IOException, ParseException {

        File file = new File("src/Number.txt");
        if (!file.exists()) {
            System.out.println("File not found.");
            System.exit(0);
        }

        Scanner sc = new Scanner(new File("src/Number.txt"));

        System.out.println("Writes out numbers from the file so the user can se them");

        int[] arr = Arrays.stream(sc.nextLine().trim().split(";")).mapToInt(Integer::parseInt).toArray();
        Arrays.sort(arr);
        int max = arr[arr.length - 1];
        int min = arr[0];


        sc.close();

        System.out.println("Smallest number: " + min);
        System.out.println("Largest number: " + max);
    }

}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文