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

Get specific information from text file

我有一个文本文件,其中有十行都包含这个确切信息“25987 20.7”。所以我的问题是,我怎样才能只取第二个数字 (20.7) 而丢弃第一个数字?

您可以使用字符串的拆分功能。

因此您将整行读入字符串变量,例如line,然后使用 line.split(" ") 和 space 作为参数。

这将 return 一个包含两个值的数组,您可以继续处理第二个值。 更多信息:Java String.split()

你在 farmer, corn, goose and fox problem, and deleted your previous question 得到很好的回答后问了这个问题,这对有类似问题的其他人或回答者付出的努力,或对你的导师来说不太公平,如果他们想看看你是否使用过任何借来的代码。为了他们的所有好处,让我 post Daniel Mclam 为了他人的利益而删除的答案:


通常我不会为这样一个措辞不佳的问题提供解决方案,但如果我理解正确的话,我发现它是一个有趣的问题。请确保下次也提供一些源代码,以便我们可以看到您尝试了什么。(查看 https://whosebug.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