简体   繁体   中英

Text file contains strings and numbers. How do I find the largest number associated with the string?

I have a Text File I am reading from

Each line has a pair (string, value) separated by a comma. How can I find the largest number and store its string in a variable for later use? The reason being because I have to add this string into a hashmap for the user to continue playing a game. The file will vary in size so there could be more pairs or less. It depends on when the user of my game wants to quit playing.

The only code I have for this is:

try
{
    File savedGame=new File("savedGame.txt");
    Scanner scan=new Scanner(savedGame);

    //something goes here? 

} 
catch(FileNotFoundException fileError)
{
   System.out.println("The file was not Found!\n " + "ERROR:" + fileError);
}

UPDATE: VIPER's approach to this was the cleanest way to achieve what I needed.

my code is now:

    try
    {
    int largest=0;
    String startingStr="";
    File savedGame=new File("savedGame.txt");
    Scanner scan=new Scanner(savedGame);
        while(scan.hasNext())
        {               
            String line=scan.nextLine();
            String tokens[]=line.split(",");
            if(Integer.parseInt(tokens[1])>largest)
            {
                largest=Integer.parseInt(tokens[1]);
                startingStr=tokens[0];
            }
        }
    }
    catch(FileNotFoundException fileError)
    {
        System.out.println("The file was not Found!\n " + "ERROR:" + fileError);
    }

Ok take a integer variable and initialize with 0 or smallest integer (if file has negative integer) then take another variable string

So what you want to do now is parse line by line and if number is greater than your integer variable put the corresponding string in the string variable; when you r done with the file you will have biggest integer and its corresponding string.

I would do it this way:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        String result = "";
        int temp = 0;
        String line = "";
        String path = "C:\\Users\\marco\\IdeaProjects\\untitled1\\src\\test.txt";
        try {
            java.io.BufferedReader fr = new java.io.BufferedReader(new java.io.FileReader(new File(path)));
            while ((line = fr.readLine()) != null) {
                String[] splitted = line.split(",");
                if(Integer.parseInt(splitted[1]) > temp){
                    temp = Integer.parseInt(splitted[1]);
                    result = line;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(result);
    }
}

I am no Java expert. But it works ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM