1,105,534 Community Members

Read integers from a file, sort, and print.

Member Avatar
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

Since I keep seeing people asking how to read the Integers from a File, I figured this simple snippet might be useful. What it does is it creates a new Scanner Object based on the File Object "test.txt" then it reads all of the Integers from that file, skipping over anything else that was found in the file, and adding the Integers to an ArrayList. It then sorts the ArrayList and prints out the sorted numbers. The file must exist otherwise this example will crash.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class IntegersFromFile {

	public static void main(String[] args) {
		Scanner file = null;
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		try {
			file = new Scanner(new File("test.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		while(file.hasNext()){
			if (file.hasNextInt()) list.add(file.nextInt());
			else file.next();
		}
		
		Collections.sort(list);
		
		for (Integer i: list) System.out.println(i);
		
	}

}
Member Avatar
kvass
Junior Poster
165 posts since Aug 2009
Reputation Points: 70 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
0
 

Out of curiosity, what sorting algorithm does the Collections class' sort method use?

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 
Member Avatar
jade_91
Junior Poster in Training
50 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok well i tried this but i want to modify it to sort text rather than numbers not sure how to do it I tried this

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner; 

 public class NameSort { 	

public static void main(String[] args) {	
	Scanner file = null;		
[ArrayList<String> list = new ArrayList<String>();]

 		try {	
		file = new Scanner(new File("names.txt"));
		} catch (FileNotFoundException e) {	
		e.printStackTrace();		} 	

	while(file.hasNext()){			
if (file.hasNextString()) list.add(file.nextString());
			else file.next();		}

 		Collections.sort(list); 		


for (String s: list) System.out.println(s); 	
	} 

}

but it doesnt want to work keeps coming back with errors any help would be much appreciated

Member Avatar
devninja
Light Poster
36 posts since Mar 2012
Reputation Points: 2 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

why do you have brackets around the arraylist decleration?

Member Avatar
tn464
Newbie Poster
1 post since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

if i were to want to get the total of the number in a files where would i insert the code

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

The read loop starts on line 20:
while(file.hasNext()){
That loop is where you'd perform any operations you want to do with each entry.

You
Post:
Start New Discussion
Tags Related to this Article