Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Java, seperate numbers from words in a string

$
0
0
I've got a text file that looks a bit like this

1 John Johnson 1245
2 Jack Jackson 4523
3 Peter Peterson 2356
.....................
1 column - id, 2 - name, 3 - last name 4 - salary
I need to read the data from the text file and then sort the names by salary. What I tried is to read the file into an array of Stings
public static void main(String[]  args){
	

int k = 6, i=0;
File file = new File("1.txt");
String[] s = new String[k];
try{
	Scanner in = new Scanner (file);

	while (in.hasNext())
{
	s[i]=in.nextLine();
  //  System.out.println(s[i]);
	i++;

	//String[] parts = s[i].split(" ");
    //String part1[] = new String[k];
    //String part2[] = new String[k];
    //part1[i]=parts[0];
    //part2[i]=parts[1];   
    }
	in.close();
}
 
catch(FileNotFoundException e){
	e.printStackTrace();
	
}



but the problem is that having an array of strings I can't get to the numbers (id and salary) in order to group them by the id or salary. What is the right way to do it? Should I use collections? How?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>