Results 1 to 2 of 2
  1. #1
    Pulse8 is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default input file into array with different parameters

    This is doing my head in.
    I have 2 different formats in a text file that i need to enter into an array
    I am using

    Java Code:
    String line = file.nextLine();
    String[] output = new String[4];
    output = line.split(":");
    One of the constructors has 3 args. The other has 4, so I'm using an if statement to guide the program into which constructor to use.

    Java Code:
    if (output[3] != null) 
    {
    	FirstClassTicket f = new FirstClassTicket(Integer.parseInt(output[0]), output[1], 
    			Integer.parseInt(output[2]), output[3].charAt(0));
    	System.out.printf("%d %s %d %s\n", f.getSeat(), f.getName(), f.getDuration(), f.getWaiter());
    	tickets.add(f);
    }
    else
    {
    	TrainTicket t = new TrainTicket(Integer.parseInt(output[0]), output[1],
                   Integer.parseInt(output[2]));    
    	System.out.printf("%d %s %d\n", t.getSeat(), t.getName(), t.getDuration());
    	tickets.add(t);
    }
    I continue to get a ArrayIndexOutOfBoundsException and I cannot figure out what to do.
    Last edited by Pulse8; 05-13-2012 at 02:16 AM. Reason: reformat

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,235
    Rep Power
    19

    Default Re: input file into array with different parameters

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 04-16-2012, 12:56 AM
  2. Replies: 4
    Last Post: 03-15-2012, 07:55 PM
  3. read input file into array
    By randoms:) in forum New To Java
    Replies: 3
    Last Post: 04-23-2011, 08:52 AM
  4. file input: array of integers
    By hannes in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 03:44 PM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 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
  •