Results 1 to 12 of 12
  1. #1
    ABMLK is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Please help, arrays

    Hi, I have a project that I have to create a separate class, then read the data from a file into an array of objects. I am getting an error here. Then I have to list the data of the array (as is), then sort it both alphabetically and numerically, which I have not even started because I cannot even get the first part to compile.

    I have been trying to work on this for days, and I just keep confusing myself even more.

    This is the class that I created.

    Java Code:
    public class Team
    {
    	//Private data fields. Useful for data hiding.
    	private String playerName;
    	private int jersNum;
    	
    	//The constructor method. More protection for data!
        public Team(String p, int n)
    	{
    		playerName = p;
    		jersNum = n;
    	}
    	
    	//Accessor for player names.
       public String getPlayerName()
       {
         return playerName;
       }
     
       //Accessor for jersey numbers.
       public int getJersNum()
       {
         return jersNum;
       }
    }
    This compiles with no errors. Ready for use....

    Then onto the Project.

    Java Code:
    public class Proj5
    {
    	public static void main(String[] args) throws IOException
    	{
    	//Set the size of the array.
    	final int NUM_PLAYERS = 6;
    	String play = "Team", num = "Jersey Number";
    	
    	//Initialize array of objects
    	Team[] p = new Team[NUM_PLAYERS];
    	
    	//Opens the file for reading.
    	File teamFile = new File("Team.txt");
    	Scanner inFile = new Scanner(teamFile);
    	
    	//Read data from file into array.
    	for(int index = 0; index < p.length; index++)
    	{
    	p[index] = new Team(inFile.nextInt(), inFile.nextLine());
    	}
    
    	//Close the file.
    	inFile.close();
    	
    	System.out.printf("%10s%20s\n", play, num);
            }
    When I try to compile I get this message..

    Proj5.java:28: cannot find symbol
    symbol : constructor Team(int,java.lang.String)
    location: class Team
    a[i] = new Team(inFile.nextInt(), inFile.nextLine());

    I am confused because every example that I see in the book or even online, it looks right. I am not even sure what this error means?

    PLEASE point me in the right direction. I would GREATLY appreciate the help. I have been trying to fix this for so long, that I just continue to confuse myself.

    Thankyou.

  2. #2
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Hi There,
    try p[index] = new Team(inFile.nextLine(),inFile.nextInt());

  3. #3
    ABMLK is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Great. Thankyou! It compiled atleast.

    When I run it though it comes up with ...

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at Proj5.main(Proj5.java:28)

    Any idea what that means?

  4. #4
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    can u post the file that u read from. ..

  5. #5
    ABMLK is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    The Team.txt file?

    If so, that file just contains this text...

    55 Daphne
    2 Oliver
    8 Ana
    53 Lindon
    72 Zachariah
    3 Albert

  6. #6
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    is ur file in the same directory with the class Team.java?

  7. #7
    ABMLK is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Yes, the class, the project and the text document are all saved in my private directory.

  8. #8
    ABMLK is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Anyone know what that scanner error is? I've finally moved on from the last problem and now it's on to a new one.

    This is like the neverending project! =(

  9. #9
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    try this

    Java Code:
    public class Proj5
    {
    	public static void main(String[] args) throws IOException
    	{
    	//Set the size of the array.
    	final int NUM_PLAYERS = 6;
    	String play = "Team", num = "Jersey Number";
    	
    	//Initialize array of objects
    	Team[] p = new Team[NUM_PLAYERS];
    	
    	//Opens the file for reading.
    	File teamFile = new File("Team.txt");
    	Scanner inFile = new Scanner(teamFile);
    	
    	//Read data from file into array.
    	for(int index = 0; index < p.length; index++)
    	{
            //check if the file has next to fetch
            if(inFile.hasNext()){
    	p[index] = new Team(inFile.nextInt(), inFile.nextLine());
    	}
    }
    
    	//Close the file.
    	inFile.close();
    	
    	System.out.printf("%10s%20s\n", play, num);
            }

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Go back to your original code, then just change this:
    Java Code:
    [COLOR="Red"]    public Team(String p, int n)
    [/COLOR]
    to this:
    Java Code:
    [COLOR="Blue"]
        public Team(int n, String p)
    [/COLOR]
    Your file is laid out as number, then name, so you need to read it that way. So you don't want to change your Scanner code, but it's simple to change your constructor.

    -Gary-

  11. #11
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    try to use hasNext() before using nextInt() or nextLine()

    try this code

    Java Code:
    Code:
    public class Proj5
    {
    	public static void main(String[] args) throws IOException
    	{
    	//Set the size of the array.
    	final int NUM_PLAYERS = 6;
    	String play = "Team", num = "Jersey Number";
    	
    	//Initialize array of objects
    	Team[] p = new Team[NUM_PLAYERS];
    	
    	//Opens the file for reading.
    	File teamFile = new File("Team.txt");
    	Scanner inFile = new Scanner(teamFile);
    	
    	//Read data from file into array.
    	for(int index = 0; index < p.length; index++)
    	{
            //check if the file has next to fetch
            if(inFile.hasNext()){
    	p[index] = new Team(inFile.nextLine(),inFile.nextInt());
    	}
    }
    
    	//Close the file.
    	inFile.close();
    	
    	System.out.printf("%10s%20s\n", play, num);
            }

  12. #12
    ABMLK is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Thank you both!. I took both of your suggestions and did both, just to be sure, and it worked!

    You've been an immense help, Thank you again.

    Hopefully the rest of the program goes smoothly.

Similar Threads

  1. Need Help with Arrays!
    By kassoli in forum New To Java
    Replies: 3
    Last Post: 06-03-2010, 06:19 AM
  2. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  3. Arrays
    By PhQ in forum New To Java
    Replies: 3
    Last Post: 04-18-2010, 09:40 PM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  5. Need help with Arrays
    By dietgal in forum New To Java
    Replies: 21
    Last Post: 10-08-2008, 02:59 PM

Posting Permissions

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