Results 1 to 10 of 10
  1. #1
    Enkie85 is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default static and non static

    I know this is a pretty common problem for people new to java but I am having some issues constructing classes in java programs. In my first java class we did very little work with classes or containers and I don't feel I got a good handle on the concept. I have been reading around online and I am stumped as to how to fix this code. The program reads a text file with 25 lines with the last name, first name, and the zipcode. I have to scan the file and then make an object containing the information for several individuals from the list and then make an object array containing the newly made objects. My plan was to read the three fields into three arrays and then I could use the index number alone to make the objects I want. I wrote the code about filling the arrays in the static main section and i get into a problem accessing the information from the class I made. It doesn't seem to be able to see the information from the main so I tried moving the code about the array into the Person class i created and that just makes even more errors. I am rereading my text book on the section on classes and containers but its not particularly helpful. This is my code thus far. This bit wont compile but if i remove the Person class it works fine. The parts i print i was just printing to make sure i had all the arrays filling correctly.

    import java.util.Scanner;
    import java.io.*;
    public class zipcodeobjects1 {
    public class Person {
    public Person(int i) {
    first = firstname[i];
    last = lastname[i];
    zip = zipcode[i];
    }
    }
    public static void main (String[] args) throws IOException {
    String[] firstname = new String[25];
    String[] lastname = new String[25];
    int[] zipcode = new int[25];
    int count = 0;
    String testconvert;
    String currentline;
    Scanner linescan, word;
    linescan = new Scanner (new File("zipandname.tst"));
    while (linescan.hasNext()) {
    currentline = linescan.nextLine();
    System.out.println("Last name, First name, Zipcode: " + currentline);
    word = new Scanner (currentline);
    while (word.hasNext()) {
    lastname[count] = word.next();
    firstname[count] = word.next();
    testconvert = word.next();
    zipcode[count] = Integer.parseInt(testconvert);
    count++;
    }
    }
    Person firstBook = new Person(2);
    }
    }

  2. #2
    Enkie85 is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    Any advice would be greatly appreciated.

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Hey welcome to the forums! Java forums have this custom tag that helps encode the indentation of a source file using the [code] tags. Like this:
    Java Code:
    import java.util.Scanner;
    import java.io.*;
    public class zipcodeobjects1 {
    	public class Person {
    		public Person(int i) {
    			first = firstname[i];
    			last = lastname[i];
    			zip = zipcode[i];
    		}
    	}
    	public static void main (String[] args) throws IOException {
    		String[] firstname = new String[25];
    		String[] lastname = new String[25];
    		int[] zipcode = new int[25];
    		int count = 0;
    		String testconvert;
    		String currentline;
    		Scanner linescan, word;
    		linescan = new Scanner (new File("zipandname.tst"));
    		while (linescan.hasNext()) {
    			currentline = linescan.nextLine();
    			System.out.println("Last name, First name, Zipcode: " + currentline);
    			word = new Scanner (currentline);
    			while (word.hasNext()) {
    				lastname[count] = word.next();
    				firstname[count] = word.next();
    				testconvert = word.next();
    				zipcode[count] = Integer.parseInt(testconvert);
    				count++;
    			}
    		}
    		Person firstBook = new Person(2);
    	}
    }
    Now with the program:
    1. The variables first, last, and zip are not initialized.
    2. How are you going to transfer the information from the file to the object values (first, last, zip)? Through the constructor of course.
    3. To make your life easier, I suggest not making the variables last, first, and zip arrays.

    I'll start you off
    Java Code:
    ...
    public class zipcodeobjects1 {
    	public class Person {
    		
    		//Step #1: Initializing the variables first, last, and zip.
    		private String first;
    		private String last;
    		private int zip;
    		
    		//Step #2:Transfer the information from the file to the
    		//Person's fields (first, last, and zip variables)
    		public Person(String firstName, String lastName, int zipCode) {
    			first = firstName;
    			last = lastName;
    			zip = zipCode;
    		}
    	}
          ....
    }
    Last edited by Lil_Aziz1; 06-20-2010 at 03:43 AM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    Enkie85 is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    Wouldnt i make a private of each at the top of the class like this? Sorry i have like 3 working versions in different places and its difficult to keep them straight at this point. I had one version with the private variables declared and it still didn't work right.
    Java Code:
    public class Person {
    private String first;
    private String last;
    private int zip;
    public Person(int i) {
    first = firstname[i];
    last = lastname[i];
    zip = zipcode[i];
    }
    }
    It is having trouble it seems finding the firstname[i] lastname[i] and zipcode[i]


    zipcodeobjects1.java:9: cannot find symbol
    symbol : variable firstname
    location: class zipcodeobjects1.Person
    first = firstname[i];
    ^
    zipcodeobjects1.java:10: cannot find symbol
    symbol : variable lastname
    location: class zipcodeobjects1.Person
    last = lastname[i];
    ^
    zipcodeobjects1.java:11: cannot find symbol
    symbol : variable zipcode
    location: class zipcodeobjects1.Person
    zip = zipcode[i];
    ^
    zipcodeobjects1.java:35: non-static variable this cannot be referenced from a static context
    Person firstBook = new Person(2);
    ^
    4 errors

  5. #5
    Enkie85 is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    I would have preferred to not do it with arrays myself but I downloaded the syllabus for the java 2 class I am taking in the fall and I planned to just write my labs over the summer so I stay frosty. The lab specifically asks to do it with arrays. I tried moving the stuff from the main up into the Person class and it just made the number of errors astronomical.

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    You should not read the data into 3 arrays but rather read in a line, create a single Person object, and place the object into a single Person array. I agree with Lil Aziz that your Person constructor should take three Strings as parameters for the constructor, that the Person class shouldn't have any array code within it. The array will be outside of this class.

    Again, use Lil Aziz's code as a starting point as he's pointing you in the right direction.

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by Enkie85 View Post
    I would have preferred to not do it with arrays myself but I downloaded the syllabus for the java 2 class I am taking in the fall and I planned to just write my labs over the summer so I stay frosty. The lab specifically asks to do it with arrays. I tried moving the stuff from the main up into the Person class and it just made the number of errors astronomical.
    The lab only asks you to create one array, a Person array, and that's the only array you should have. Period.

    Also, if you have an "astronomical" number of errors, then your coding technique needs to be changed. You should only write a small amount of code at a time, compile this code, and not add any more code until any compile errors that are found are corrected. This way you'll avoid having a ton of errors, trust me.

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Oh, also, the Person class should be in its own file. It should be a stand-alone class and should not be nested within zipcodeobjects1 or any other class.

  9. #9
    Enkie85 is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    no I just have the 4 errors. I have fixed the code by not using the arrays and making it significantly less complicated and it works now. I just want to know now why this code wont work specifically. Can classes not receive parameters that are generated in the main?

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by Enkie85 View Post
    no I just have the 4 errors. I have fixed the code by not using the arrays and making it significantly less complicated and it works now.
    Most excellent!

    I just want to know now why this code wont work specifically. Can classes not receive parameters that are generated in the main?
    It's a matter of scope. The arrays that you are trying to reference within the Person class are visible only in the main method where they were declared. Their scope does not extend to within the Person class. Even if it did, though, it would be a bad design, and again Lil Aziz's code shows a much better way. But regardless, I'm glad that you've got it working now.

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 11:12 AM
  2. Replies: 4
    Last Post: 05-12-2010, 11:05 AM
  3. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 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
  •