有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从文本文件中获取特定信息

我有一个文本文件,它有十行,其中都包含这个确切的信息“2598720.7”。所以我的问题是,我怎么能只接受第二个数字(20.7)而放弃第一个


共 (2) 个答案

  1. # 1 楼答案

    您可以使用字符串的拆分函数

    因此,可以将整行读入字符串变量,例如line,然后使用带有空格的line.split(" ")作为参数

    这将返回一个包含两个值的数组,您可以继续第二个值。 有关进一步信息:Java String.split()

  2. # 2 楼答案

    您询问了farmer, corn, goose and fox problem,并在得到很好的回答后删除了您的previous question,这对于其他有类似问题的人或回答者付出的努力来说是不公平的,或者对于您的指导者来说,如果他们希望查看您是否使用了任何借用的代码,那么他们也不太公平。为了他们的利益,让我为其他人的利益发布丹尼尔·麦克拉姆删除的答案:


    通常我不会为这样一个措辞拙劣的问题提供解决方案,但如果我理解正确,我发现这是一个有趣的问题。请确保下次还提供一些源代码,以便我们可以看到您尝试了什么。(查看https://stackoverflow.com/help/how-to-ask,了解如何提出一个好问题)

    public static void main(String[] args)
    {
        //scanner object for user input
        Scanner input=new Scanner(System.in);
    
        //define objects
        //Use true and false to determine the side
        boolean farmer=false;
        boolean fox=false;
        boolean goose=false;
        boolean corn=false;
    
        //Show rules
        System.out.println("Welcome to the Farmer, Goose, Fox, Corn Problem.");
        System.out.println("The Fox and Goose cannot be left alone.");
        System.out.println("The Goose and Corn cannot be left alone.");
        System.out.println("You may only bring one object across the river at a time.");
    
        //loop flag
        boolean isFinished=false;
    
        while(!isFinished)
        {
            //show objects on the same side as the farmer
            System.out.println("\nThe objects on your side of the river are:");
            if(farmer==fox)
            {
                System.out.println("1.Fox ");
            }
            if(farmer==goose)
            {
                System.out.println("2.Goose ");
            }
            if(farmer==corn)
            {
                System.out.println("3.Corn ");
            }
    
            System.out.println("4.Nothing ");
    
            //get user selection and validate
            //just makes sure that the object is on the same side as the farmer
            //if true move the object then test if valid after switch
    
            boolean isValidInput=false;
            int userSelection;
            do
            {
                System.out.print("\nEnter the number for the object you wish to move:");
                userSelection=input.nextInt();
                if(userSelection==1)
                {
                    if(fox==farmer)
                    {
                        farmer=!farmer;
                        fox=!fox;
                        isValidInput=true;
                    }
                }
                else if(userSelection==2)
                {
                    if(goose==farmer)
                    {
                        farmer=!farmer;
                        goose=!goose;
                        isValidInput=true;
                    }
                }
                else if(userSelection==3)
                {
                    if(corn==farmer)
                    {
                        farmer=!farmer;
                        corn=!corn;
                        isValidInput=true;
                    }
                }
                else if(userSelection==4)
                {
                        farmer=!farmer;
                        isValidInput=true;
                }
                else
                {
                    isValidInput=false;
                }
            }while(!isValidInput);
    
    
            //check solution 
            boolean isValid=false;
    
            if((fox==goose && farmer!=fox))
            {
                System.out.println("\nFox and goose cannot stay together.");
            }
            else if(goose==corn && farmer!=goose)
            {
                System.out.println("\nGoose and corn cannot stay together.");
            }
            else
            {
                isValid=true;
            }
    
    
            //if the solution is not valid switch objects back and request user input again
            if(!isValid)
            {
                if(userSelection==1)
                {
                    if(fox==farmer)
                    {
                        farmer=!farmer;
                        fox=!fox;
                        isValidInput=true;
                    }
                }
                else if(userSelection==2)
                {
                    if(goose==farmer)
                    {
                        farmer=!farmer;
                        goose=!goose;
                        isValidInput=true;
                    }
                }
                else if(userSelection==3)
                {
                    if(corn==farmer)
                    {
                        farmer=!farmer;
                        corn=!corn;
                        isValidInput=true;
                    }
                }
                else if(userSelection==4)
                {
                        farmer=!farmer;
                        isValidInput=true;
                }
                else
                {
                    isValidInput=false;
                }
            }
    
            //check if final solution achieved
            if(fox && goose && corn && farmer)
            {
                System.out.println("\nYou Win, Good Job!!!");
                isFinished=true;
            }
    
        }//end while
    
    }   //end main