Need assistance with arrays

R

RookThis

I'm new to Java and trying to understand the array process. I have
file that I am trying to read in and populate an array with the
data. I have this so far, but still having problems. Can someone
tell me what I'm doing wrong? Thank you!

public class test
{
public static void main (String [] args)throws Exception
{
Scanner ifile1 = new Scanner(new File("input.txt"));
String type = " ";
String color = " ";
String description = " ";
String make = " ";
int ccount = 0;
int index = 0;
carFile[] items = new carFile[50];
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);
items[index].setColor(color);
items[index].setDescription(description);
items[index].setMake(make);
index++;
}
ifile1.close();

}
}
 
P

Patricia Shanahan

RookThis said:
I'm new to Java and trying to understand the array process. I have
file that I am trying to read in and populate an array with the
data. I have this so far, but still having problems. Can someone
tell me what I'm doing wrong? Thank you!

public class test
{
public static void main (String [] args)throws Exception
{
Scanner ifile1 = new Scanner(new File("input.txt"));
String type = " ";
String color = " ";
String description = " ";
String make = " ";
int ccount = 0;
int index = 0;
carFile[] items = new carFile[50];

This creates an array of 50 null carFile references.
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);

You need to make items[index] point to an object, instead of being null,
before you can operate on the object it points to. Perhaps:

items[index] = new carFile();

before this line.
items[index].setColor(color);
items[index].setDescription(description);
items[index].setMake(make);
index++;
}
ifile1.close();

}
}
 
R

RookThis

RookThis said:
I'm new to Java and trying to understand the array process. I have
file that I am trying to read in and populate an array with the
data. I have this so far, but still having problems. Can someone
tell me what I'm doing wrong? Thank you!
public class test
{
public static void main (String [] args)throws Exception
{
Scanner ifile1 = new Scanner(new File("input.txt"));
String type = " ";
String color = " ";
String description = " ";
String make = " ";
int ccount = 0;
int index = 0;
carFile[] items = new carFile[50];

This creates an array of 50 null carFile references.
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);

You need to make items[index] point to an object, instead of being null,
before you can operate on the object it points to. Perhaps:

items[index] = new carFile();

before this line.


items[index].setColor(color);
items[index].setDescription(description);
items[index].setMake(make);
index++;
}
ifile1.close();
}
}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks for the response, I really appreciate the assistance. Are you
saying to enter that line as the first line after the while statement?
 
E

Eric Sosman

RookThis said:
I'm new to Java and trying to understand the array process. I have
file that I am trying to read in and populate an array with the
data. I have this so far, but still having problems. Can someone
tell me what I'm doing wrong? Thank you!

It's usually a good idea to describe the nature of the
problems you're having. In the case at hand I can make a
pretty good guess, but guesses aren't always accurate; next
time (or even this time!), you may find that people guess
wrong and give you lots of advice about problems you're *not*
having ...
carFile[] items = new carFile[50];

This creates the items array and fills it with fifty
null reference values. It does *not* create fifty carFile
objects for them to refer to, so ...
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);

.... right here you get a NullPointerException. The value
of items[index] is null; it does not refer to an actual
carFile object. You need to create a carFile object for it
to refer to, perhaps by inserting

items[index] = new carFile();

just before this line (what you actually insert will depend on
what the carFile constructor requires).

By the way, it is customary for the names of classes and
interfaces to begin with upper-case letters: `Test' instead of
`test', `CarFile' instead of `carFile'.
 
R

RookThis

RookThis said:
I'm new to Java and trying to understand the array process. I have
file that I am trying to read in and populate an array with the
data. I have this so far, but still having problems. Can someone
tell me what I'm doing wrong? Thank you!

It's usually a good idea to describe the nature of the
problems you're having. In the case at hand I can make a
pretty good guess, but guesses aren't always accurate; next
time (or even this time!), you may find that people guess
wrong and give you lots of advice about problems you're *not*
having ...
carFile[] items = new carFile[50];

This creates the items array and fills it with fifty
null reference values. It does *not* create fifty carFile
objects for them to refer to, so ...
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);

... right here you get a NullPointerException. The value
of items[index] is null; it does not refer to an actual
carFile object. You need to create a carFile object for it
to refer to, perhaps by inserting

items[index] = new carFile();

just before this line (what you actually insert will depend on
what the carFile constructor requires).

By the way, it is customary for the names of classes and
interfaces to begin with upper-case letters: `Test' instead of
`test', `CarFile' instead of `carFile'.

This is what I get when I run it:


test.java:16: cannot find symbol
symbol : class CarFile
location: class Test
CarFile[] items = new CarFile[50];
^
test.java:16: cannot find symbol
symbol : class CarFile
location: class Test
CarFile[] items = new CarFile[50];
^
test.java:19: cannot find symbol
symbol : method CarFile()
location: class Test
items[index] = CarFile();
^
4 errors
 
L

Lars Enderin

RookThis skrev:
RookThis said:
I'm new to Java and trying to understand the array process. I have
file that I am trying to read in and populate an array with the
data. I have this so far, but still having problems. Can someone
tell me what I'm doing wrong? Thank you!
It's usually a good idea to describe the nature of the
problems you're having. In the case at hand I can make a
pretty good guess, but guesses aren't always accurate; next
time (or even this time!), you may find that people guess
wrong and give you lots of advice about problems you're *not*
having ...
carFile[] items = new carFile[50];
This creates the items array and fills it with fifty
null reference values. It does *not* create fifty carFile
objects for them to refer to, so ...
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);
... right here you get a NullPointerException. The value
of items[index] is null; it does not refer to an actual
carFile object. You need to create a carFile object for it
to refer to, perhaps by inserting

items[index] = new carFile();

just before this line (what you actually insert will depend on
what the carFile constructor requires).

By the way, it is customary for the names of classes and
interfaces to begin with upper-case letters: `Test' instead of
`test', `CarFile' instead of `carFile'.

This is what I get when I run it:


test.java:16: cannot find symbol
symbol : class CarFile
location: class Test
CarFile[] items = new CarFile[50];
^
test.java:16: cannot find symbol
symbol : class CarFile
location: class Test
CarFile[] items = new CarFile[50];
^
test.java:19: cannot find symbol
symbol : method CarFile()
location: class Test
items[index] = CarFile();
^
4 errors

You have to change the declaration of carFile to CarFile (also the file
name to CarFile.java). Case counts in Java.
 
R

RookThis

RookThis skrev:




RookThis wrote:
I'm new to Java and trying to understand the array process. I have
file that I am trying to read in and populate an array with the
data. I have this so far, but still having problems. Can someone
tell me what I'm doing wrong? Thank you!
It's usually a good idea to describe the nature of the
problems you're having. In the case at hand I can make a
pretty good guess, but guesses aren't always accurate; next
time (or even this time!), you may find that people guess
wrong and give you lots of advice about problems you're *not*
having ...
carFile[] items = new carFile[50];
This creates the items array and fills it with fifty
null reference values. It does *not* create fifty carFile
objects for them to refer to, so ...
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);
... right here you get a NullPointerException. The value
of items[index] is null; it does not refer to an actual
carFile object. You need to create a carFile object for it
to refer to, perhaps by inserting
items[index] = new carFile();
just before this line (what you actually insert will depend on
what the carFile constructor requires).
By the way, it is customary for the names of classes and
interfaces to begin with upper-case letters: `Test' instead of
`test', `CarFile' instead of `carFile'.
This is what I get when I run it:
test.java:16: cannot find symbol
symbol : class CarFile
location: class Test
CarFile[] items = new CarFile[50];
^
test.java:16: cannot find symbol
symbol : class CarFile
location: class Test
CarFile[] items = new CarFile[50];
^
test.java:19: cannot find symbol
symbol : method CarFile()
location: class Test
items[index] = CarFile();
^
4 errors

You have to change the declaration of carFile to CarFile (also the file
name to CarFile.java). Case counts in Java.- Hide quoted text -

- Show quoted text -

I made the suggested changes, but it didn't make a difference.
Anything else I can try to get this to work? Thanks for the
suggestions though.
 
L

Lew

RookThis said:
I made the suggested changes, but it didn't make a difference.
Anything else I can try to get this to work? Thanks for the
suggestions though.

Hard to make suggestions without seeing a complete example of the current
state of affairs, but let me try - after pointing you to
<http://www.physci.org/codes/sscce.html>
for next time.

package example;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Car
{
private String type;
private String color;
private String description;
private String make;

public void setType( String val ) { type = val; }
public String get() { return type; }

public void setColor( String val ) { color = val; }
public String get() { return color; }

public void setDescription( String val ) { description = val; }
public String get() { return description; }

public void setMake( String val ) { make = val; }
public String get() { return make; }

public static void main( String [] args )
{
if ( args.length < 2 )
{
System.err.println( "Wrong args" );
return;
}

Scanner carIn;
try
{
carIn = new Scanner( new BufferedReader( new FileReader( args [0] )));
}
catch ( IOException ex )
{
System.err .println( "Bad File "+ args [0] +". "+ ex.getMessage() );
ex.printStackTrace( System.err );
return;
}

Car[] cars;
{
int nc;
try
{
nc = Integer.parseInt( args [1] );
if ( nc < 0 )
{
nc = 50;
}
}
catch ( NumberFormatException ex )
{
nc = 50;
}
cars = new Car [nc];
}

try
{
for ( int ix = 0; ix < cars.length && carIn.hasNext(); ++ix )
{
Car car = new Car();

String val = carIn.next();
car.setType( val );

val = carIn.next();
car.setColor( val );

val = carIn.next();
car.setDescription( val );

val = carIn.nextLine();
car.setMake( val );

cars [ix] = car;
}

// do something here with cars
}
finally
{
carIn.close();
}
}

}
 
L

Lew

Lew said:
public void setType( String val ) { type = val; }
public String get() { return type; }

public void setColor( String val ) { color = val; }
public String get() { return color; }

public void setDescription( String val ) { description = val; }
public String get() { return description; }

public void setMake( String val ) { make = val; }
public String get() { return make; }

Oops - all those get() calls should've been getX() where X in { Type, Color,
Description, Make }
 
P

Patricia Shanahan

RookThis said:
RookThis said:
I'm new to Java and trying to understand the array process. I have
file that I am trying to read in and populate an array with the
data. I have this so far, but still having problems. Can someone
tell me what I'm doing wrong? Thank you!
It's usually a good idea to describe the nature of the
problems you're having. In the case at hand I can make a
pretty good guess, but guesses aren't always accurate; next
time (or even this time!), you may find that people guess
wrong and give you lots of advice about problems you're *not*
having ...
carFile[] items = new carFile[50];
This creates the items array and fills it with fifty
null reference values. It does *not* create fifty carFile
objects for them to refer to, so ...
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);
... right here you get a NullPointerException. The value
of items[index] is null; it does not refer to an actual
carFile object. You need to create a carFile object for it
to refer to, perhaps by inserting

items[index] = new carFile();

just before this line (what you actually insert will depend on
what the carFile constructor requires).

By the way, it is customary for the names of classes and
interfaces to begin with upper-case letters: `Test' instead of
`test', `CarFile' instead of `carFile'.

This is what I get when I run it:


test.java:16: cannot find symbol
symbol : class CarFile
location: class Test
CarFile[] items = new CarFile[50];
^
test.java:16: cannot find symbol
symbol : class CarFile
location: class Test
CarFile[] items = new CarFile[50];
^
test.java:19: cannot find symbol
symbol : method CarFile()
location: class Test
items[index] = CarFile();
^
4 errors

You did not actually *need* to change carFile. Using a capital letter at
the start of a class or interface name is a convention, not a
requirement. Following it will make it easier for other Java programmers
to read your code.

However, you absolute *must* pick a single spelling for each class name,
including which letters are capitalized.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top