Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Error Constructing an Object

    I'm struggling with an assignment for my Programming class. I'm trying to make an array of objects using a class I named Person. Each object has a name and a number. Right now, I have a mistake that results in each object not getting a name. So when I try to print each objects' name, it prints null.

    My comments at the beginning of my program that defines the objectives:
    Code:
    	This program requests a file name from the user. If the presented name is not found, loop until they enter either an asterisk
    	or a correct file name. Quit the program if an asterisk is entered. 
    	1. For every line, if the data element after the comma is not a valid floating-point number, skip the line and note the error. Add 
    	the number to the running sum if it is valid.
    	2. Display the sum of valid numbers, the average, and the total number of valid lines at the end of each.
    	3. Display the total number of lines that contained invalid values.
    My code:
    Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class floatingSum
    {
    	public static void main(String[] args)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		int invalid, valid; 		
    		float runningSum; 
    		Person[] person = new Person[22];
    		while(true)
    		{
    			System.out.println("Enter a file name. ");
    			String fileName = keyboard.nextLine();
    		
    			try
    			{
    				if(fileName.charAt(0) == '*' && fileName.length() == 1)
    					throw new RuntimeException("Program commanded to end");
    			}
    			catch (RuntimeException ex)
    			{
    				System.out.println("The program will now end.");
    				System.exit(0);
    			}
    			
    			try
    			{
    				Scanner inputFile = new Scanner(new File(fileName));
    				while(inputFile.hasNext())
    				{
    					for(int row = 0; row < 22; row++)
    					{
    						String name, number;
    						String line = inputFile.nextLine();
    						String delim = ",";
    						String[] temp = line.split(delim);
    						try
    						{
    							name = temp[0];
    							number = temp[1];
    							throw new RuntimeException("Missing Comma");
    						}
    						catch (RuntimeException ex)
    						{
    							name = temp[0];
    							number = "0";
    						}
    						person[row] = new Person(name, number);
    						System.out.println(person[row].name);		//to check whats in the array
    					}
    				}
    				inputFile.close();
    			}
    			catch (FileNotFoundException ex)
    			{
    				System.out.println("Exception: " + fileName + " not found.");
    			}
    		}
    	}
    }
    	
    class Person
    {
    	String name, number;
    	
    	Person()
    	{
    	}
    	
    	Person(String name, String number)
    	{
    		name = name;
    		number = number;
    	}
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I just rebuilt my system and haven't put javac back on yet, but it looks to me that its simply a scope issue:
    PHP Code:
        Person(String nameString number)
        {
            
    name name;
            
    number number;
        } 
    'name' is 'name', not person.name. Reference the instance using this.name = name; this.number = number; and it should work fine.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I really appreciate your time and help!
    Last edited by trantommyd; 04-07-2012 at 06:59 AM.

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    97
    Thanks
    1
    Thanked 13 Times in 13 Posts
    Have you tried building a constructor and using getters and setters?

    for example:
    Code:
    public Person(String name, String number)
    {
          private String name;
          private String number;
    }
    
    public void setName(String name)
    {
          this.name = name;
    }
    
    public String getName()
    {
          return name;
    }
    
    public void setNumber(String number)
    {
          this.number = number;
    }
    
    public String getNumber()
    {
          return number;
    }


  •  

    Posting Permissions

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