Results 1 to 4 of 4

Thread: Sorting

  1. #1
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default Sorting

    Hello, im trying to get some information about soccer players from a text file and put it into an object. This object will be added to an arraylist, and the arraylist will be sorted. The program has to first ask for the name of the txt file where it's going to fine get the info (name of player, team of player, and # of goals). But i have an error wich I can't find, I would apreciate if anyone can help me to see what it is.The program only asks be for the name of the txt file and after that, the erroris displayed.
    Data class, the arraylist is here
    Java Code:
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    
    public class Data {
    List<Player> league = new ArrayList<Player>();
    Player eachPlayer=null;
    Player tempPlayer;
    	public void importFile(String f) throws IOException{
    		tittle();
    		String fileName=f;
    		Scanner myScanner = new Scanner(new File(fileName));
    		boolean line=true;
    	  	while ((line=myScanner.hasNextLine())!=false){
    	  		tempPlayer=new Player(myScanner.nextLine(),myScanner.nextLine(), myScanner.nextInt());
    	  		myScanner.nextLine();
    	  		league.add(tempPlayer);
    	  	}
    	}
    	public String getFileName(){
    		Scanner input=new Scanner(System.in);
    		System.out.print("Enter the name of the file: ");
    		String name=input.nextLine();
    		return name;
    	}
    	public void tittle(){
    		System.out.println("TOP 5 GOAL SCORERS MEXICAN SOCCER");
    		System.out.println("Name  /  Team  / Goals");
    		System.out.println("----------------------------------");
    	}
       public void add (Player eachPlayer){
       	league.add(eachPlayer);
       }
       public void list(){
       	for(Player eachPlayer:league){
       		System.out.println (eachPlayer.getName()+ " / " + eachPlayer.getTeam() + " / " + eachPlayer.getGoals());
       	}
       }
    Encapsulate, the main is here
    Java Code:
    import java.util.*;
    import java.io.File;
    import java.io.IOException;
    public class Encapsulate {
    	public static void main (String[] args) throws IOException{
    		Data top=new Data();
    		top.importFile(top.getFileName());
    		top.list();
    	}
    }
    txt file
    Java Code:
    Bob Buker
    Tigres
    4
    Pudry Puker
    Rayados
    2
    Pony Ruiz
    Santos
    10
    Christian Benitez
    Santos 
    40
    Matias Baboso
    America
    1
    error message
    Java Code:
    Exception in thread "main" java.io.FileNotFoundException: players (El sistema no puede encontrar el archivo especificado)
    	at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(Unknown Source)
    	at java.util.Scanner.<init>(Unknown Source)
    	at Data.importFile(Data.java:22)
    	at Encapsulate.main(Encapsulate.java:7)
    I also have a player class, but I didn't included it because it only stores the information. I'm using eclipse as my IDE and the txt file is stored in the src of the project. If you have any question, just ask it.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Your file is not in your working directory. One way to get around this is to tyupe in the full path to the file. eg C:/Full/Path/To/file.txt

    By the way what has this got to do with Sorting as per your topic?

  3. #3
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default

    Because I want to sort the arraylist but i haven't made that method yet because i want to display it without sorting first, but i have that error, can u explain me how to type the full path to the file more detailed please?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Your getFileName method prompts the user to enter a file name. So enter the full path. Of course this is not an ideal solution. Alternatively you can use a JFileChooser.

Similar Threads

  1. little help with sorting
    By drgnfire25 in forum New To Java
    Replies: 3
    Last Post: 01-15-2011, 09:23 PM
  2. Help in sorting
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 06:15 PM
  3. sorting problem
    By vasug in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2010, 04:55 AM
  4. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •