Help Please Arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Help Please Arrays

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Question Help Please Arrays

    I'm having some trouble, In this program I want it to read input from textboxes and store them into an array, I then want to later on print out what's in the array, When It prints Out i'm getting Junk, It's not printout out in the array, I figure I have to either convert the int's to strings, But I don't know how to do This, Can someone help.

    String who = event.getActionCommand();
    String stname = txtname.getText();
    String strunning = txtrunning.getText();
    String styear = txtyear.getText();

    // Creating arrays out of user input
    String [] name = {stname};
    for (int i = 0; i < name.length; i ++) {
    stname = name[i];
    }
    String [] year = {styear};
    for (int i = 0; i < year.length; i ++) {
    styear = year[i];
    }
    String [] running = {strunning};
    for (int i = 0; i < running.length; i ++) {
    strunning = running[i];
    }


    try {
    BufferedReader read = new BufferedReader(new FileReader("movie.txt"));
    PrintWriter fil = new PrintWriter(new FileWriter("movie.txt"));

    String line = read.readLine();

    if (line == null) {
    fil.println("Movie Title: " + name);
    fil.println("Movie Running Time: " + running);
    fil.println("Movie Year Was Made: " + year);
    }else {
    System.out.println ("Data is being overwritten");
    fil.println("Movie Title: " + name);
    fil.println("Movie Running Time: " + running);
    fil.println("Movie Year Was Made: " + year);
    }
    read.close();
    fil.close();
    }catch (IOException ioe) { }

    What's Being Printed Out

    Movie Title: [Ljava.lang.String;@171732b
    Movie Running Time: [Ljava.lang.String;@1404536
    Movie Year Was Made: [Ljava.lang.String;@7fdcde

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    First things first, this code:

    String [] name = {stname};
    for (int i = 0; i < name.length; i ++) {
    stname = name[i];
    }

    creates an array of _one_ element, the stname, then it loops the array (once of course) and stores that element in stname, where it already is.....

    and a statement like:

    fil.println("Movie Title: " + name);

    Prints out the array: name, using the toString() method of the array, and that looks like junk since it is the address of the first element in the array, not of any individualt string in the array.

    Apart from that I have difficulties in seing how the first part of your code connects to the BufferedReader part...
    eschew obfuscation

  3. #3
    Join Date
    Sep 2004
    Posts
    103
    So how can I fix it, how would you do it?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I won't code the whole shebang here but I can give you an idea: First of all, when you are dealing w. the problem where you don't know how many of something you'll get, then use an ArrayList, not an array - where you have to know in advance how many elements you'll be getting. So You just declare an Arraylist object as a class variable say:
    ArrayList repList=new ArrayList();

    In the eventhandler where you get the textbox entries you collect these like:
    repList.add(aTextBox.getText().trim());

    And in the listout method you do:

    Code:
    for (int i=0; i<repList.size(); i++) {
      String aString=(String)repList.get(i); //<-- typecasting
      System.out.println("reply no:"+i+" is: "+aString);
    }
    Note: the ArrayList deals w. Objects, it doesn't understand anything else.... so you have to typecast the elements to Strings when you retrieve them from the list.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center