...

View Full Version : Error Constructing an Object



trantommyd
04-07-2012, 05:35 AM
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:


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:


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;
}
}

Fou-Lu
04-07-2012, 06:31 AM
I just rebuilt my system and haven't put javac back on yet, but it looks to me that its simply a scope issue:


Person(String name, String 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.

trantommyd
04-07-2012, 07:47 AM
I really appreciate your time and help!

sean3838
04-08-2012, 02:37 PM
Have you tried building a constructor and using getters and setters?

for example:


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;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum