Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: all objects in array the same?

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default all objects in array the same?

    in this code:
    import java.util.*;
    import java.io.*;
     
    public class FinalProject
    {
      public static void main (String[] args)
      {
        Scanner in = new Scanner(System.in);
        System.out.println("how many pages do you want?");
        int pages = in.nextInt();
        Html[] site = new Html[pages];
        for(int i = 0; i < pages; i++)
        {
          if(i==0)
          {
            System.out.println("black box line");
            String blkBox = in.nextLine();
            System.out.println("what do want the site titled?");
            String title1 = in.nextLine();
            System.out.println("what do you want on the home page?");
            String content = in.nextLine();
            System.out.println("what image do you want behind the page?");
            String image = in.nextLine();
            boolean home = true;
            site[0] = new Html(title1, content, image, home);
            System.out.println("bong " + site[i].titleGet() + " " + i);
          }
          else if(i>0)
          {
            System.out.println("what do you want page " + i + " titled?");
            String title = in.nextLine();
            System.out.println("what do you want on page" + i + "?");
            String content = in.nextLine();
            System.out.println("what image do you want behind the page?");
            String image = in.nextLine();
            boolean home = false;
            site[i] = new Html(title, content, image, home); 
            System.out.println("bong " + site[0].titleGet() + " " + i);
            System.out.println("bong " + site[i].titleGet() + " " + i);
          }
          else
          {
            System.out.println("bing");
          }
        }
        for(int j = 0; j < pages; j++)
        {
          if(j == 0)
          {
            try {
              BufferedWriter out = new BufferedWriter(new FileWriter(site[0].titleGet() + ".html"));
              out.write(site[0].homeGet(pages, site[0].titleGet()));
              out.close();
              System.out.println("ding " + site[j].titleGet() + " " + j);
            } catch (IOException e) {
            }
          }
          else if(j > 0)
          {
            try {
              BufferedWriter out = new BufferedWriter(new FileWriter(site[0].titleGet() + j + ".html"));
              out.write(site[j].pageGet());
              out.close();
              System.out.println("ding " + site[j].titleGet() + " " + j);
            } catch (IOException e) {
            }
          }
          else
          {
          }
        }
      }
    }

    using this object:
    public class Html
    {
      static String title, content, image;
      static boolean home;
     
      public Html(String t, String c, String i, boolean h)
      {
        title = t;
        content = c;
        image = i;
        home = h;
      }
      public String titleGet()
      {
        return(title);
      }
      public String pageGet()
      {
        return("<html><head><title>" + title + "</title></head><body background=\"" + image + "\"><h1 align=\"center\" font color=\"#00FF00\">" + content + "</h1></body></html>");
      }
      public String homeGet(int pages, String title)
      {
        String index = "";
        for(int j = pages -1; j > 0; j--)
        {
          index = index + "<a  font color=\"#00FF00\" href=\"" + title + j + ".html\">" + "page" + j + "</a> <br />";
          System.out.println("dong " + j);
        }
         return("<html><head><title>" + title + "</title></head><body background=\"" + image + "\"><h1 align=\"center\" font color=\"#00FF00\">" + content + "</h1> <br />" + index + "</body></html>");
      }
    }


    all objects in the site array are writen over by the newest entry, why?

    thanks
    Last edited by abrohm; June 16th, 2011 at 08:17 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: all objects in array the same?

    Do you understand what static means when a variable is declared as static?
    There is only ONE copy for all instances of the class.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: all objects in array the same?

    C:\Users\Alex\programs\assingment one\task one\final>java FinalProject
    how many pages do you want?
    3
    black box line
    what do want the site titled?
    index
    what do you want on the home page?
    content test
    what image do you want behind the page?
    test.jpg
    bong index 0
    bong index 1
    what do you want page 1 titled?
    test one
    what do you want on page1?
    test content one
    what image do you want behind the page?
    test.jpg
    bong test one 1
    bong test one 1
    bong test one 2
    what do you want page 2 titled?
    test two
    what do you want on page2?
    test content two
    what image do you want behind the page?
    test.jpg
    bong test two 2
    bong test two 2
    dong 2
    dong 1
    ding test two 0
    ding test two 1
    ding test two 2

    note: the ding, dong ect lines where for troubleshooting purposes

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: all objects in array the same?

    What is the purpose of the last post?
    You need to add an explanation of what you have posted.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: all objects in array the same?

    that was the output from command line, the ding lines showing the fact that it is over writing previous entries, the top of each pair being title of site[0] and lower being that of site[i]

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: all objects in array the same?

    You have already been given an explanation in the first post. What about that post did you not understand?

  7. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: all objects in array the same?

    Norm's telling you that by declaring the Html class variables 'static', you ensure they are shared by all instances of the class, instead of each instance having it's own variables.

    Incidentally, rather than have loops that do one thing for the first item and something different for all the others, it's simpler, clearer, and more efficient to handle the first item outside the loop, then enter the loop with a start value of 1 instead of 0.

    Just sayin'
    Last edited by dlorde; June 17th, 2011 at 11:24 AM.

Similar Threads

  1. instantiating class objects from an array
    By BadAnti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 12th, 2011, 03:27 PM
  2. How to store objects from a class inn an array?
    By dironic88 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 7th, 2011, 02:42 PM
  3. [SOLVED] Array of objects, invoking constructor for one changes others
    By BigFoot13 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 24th, 2010, 01:30 PM
  4. FAQ: find variable in an array of objects
    By dalek in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 12:40 PM
  5. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM