Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Need assistance with arrays

Thread view: 
RookThis - 17 Nov 2007 16:17 GMT
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();

     }
}
Patricia Shanahan - 17 Nov 2007 16:23 GMT
> 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
[quoted text clipped - 13 lines]
>          int index = 0;
>          carFile[] items = new carFile[50];

This creates an array of 50 null carFile references.

>          while (ifile1.hasNext())
>              {
[quoted text clipped - 3 lines]
>                  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);
[quoted text clipped - 5 lines]
>       }
> }
RookThis - 17 Nov 2007 16:29 GMT
> > 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
[quoted text clipped - 44 lines]
>
> - 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?
Eric Sosman - 17 Nov 2007 16:32 GMT
> 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())
>              {
[quoted text clipped - 3 lines]
>                  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'.

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

RookThis - 17 Nov 2007 16:44 GMT
> > 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
[quoted text clipped - 39 lines]
> Eric Sosman
> esos...@ieee-dot-org.invalid

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
Lars Enderin - 17 Nov 2007 17:13 GMT
RookThis skrev:
>>> 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
[quoted text clipped - 55 lines]
>                                 ^
> 4 errors

You have to change the declaration of carFile to CarFile (also the file
name to CarFile.java). Case counts in Java.
RookThis - 17 Nov 2007 18:43 GMT
> RookThis skrev:
>
[quoted text clipped - 62 lines]
>
> - 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.
Lew - 17 Nov 2007 19:31 GMT
> 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();
    }
  }

}

Signature

Lew

Lew - 17 Nov 2007 19:33 GMT
>   public void setType( String val )        { type = val; }
>   public String get()     { return type; }
[quoted text clipped - 7 lines]
>   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 }

Signature

Lew

Patricia Shanahan - 17 Nov 2007 21:25 GMT
>>> 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
[quoted text clipped - 55 lines]
>                                 ^
> 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
Roedy Green - 19 Nov 2007 08:57 GMT
On Sat, 17 Nov 2007 08:17:17 -0800 (PST), RookThis
<notagainok@yahoo.com> wrote, quoted or indirectly quoted someone who
said :

>I'm new to Java and trying to understand the array process
see http://mindprod.com/jgloss/array.html for an introduction to the
basics.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.