It is currently Fri Nov 01, 2024 12:00 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post Post subject: Java
 
Offline
Unstoppable
Unstoppable
Years of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membership

Joined: Sun Nov 15, 2009 8:40 pm
Posts: 1038
Karma: 10
main.java
Code:
package simpleobjects;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Main {

Object[] people = new Object[4];

    static void main(String[] args) {
       
        int i = 0;
        Object[] people = new Object[4];

        people[0] = new Object("Bill",4);
        people[1] = new Object("Nye",3);
        people[2] = new Object("Science",7);
        people[3] = new Object("Guy",3);

        while(i <= 3 )
        {
             people[i].getInformation();
             i++;
        }
        writeXML();
    }
   
    public static void writeXML()
    {

        String topLevel = "userList";
        String secondLevel = "people";
        try
        {
            BufferedWriter saveFile = new BufferedWriter(new FileWriter("1.xml"));

            saveFile.write("<" + topLevel + ">\n");

            for (int i = 0; i <= 3 ; i++)
            {
            saveFile.write("    <" + "people" + ">\n");
            saveFile.write("        <name> " +  people[i].getName() + "</name>\n");
            saveFile.write("        <number> " + "" + "</number>\n");
            saveFile.write("    </" + secondLevel + ">\n");
            }
            saveFile.write("</" + topLevel + ">\n");
           
            saveFile.close();
        }
        catch (IOException e)
        {
            System.out.println(e);
        }


    }
}

Object.java
Code:
package simpleobjects;

/**
*
* @author harry
*
*/

public class Object
{
    public int number;
    public String word;

    public Object(String inWord,int inNumber)
    {
            number = inNumber;
            word = inWord;
    }

    public int setNumber( int inNumber )
    {
        return this.number = inNumber;
    }
    public void getNumber()
    {
        System.out.println(number);
    }
    public String setWord( String inWord )
    {
        return this.word = inWord;
    }
    public void getWord()
    {
        System.out.println(word);
    }
    public void getInformation()
    {
        System.out.println("Name: " + word);
        System.out.println("Number: " + number);
    }

}


I successfully create the objects, and can pull/push data into each object but I cannot reference them in the XML function (to get the name/number of each object and save them to XML).
I've googled the error (even though it's a general error), so see how it can be fixed - with no avail.

So where am I going wrong?

Easier to download the whole package?

_________________
Skillers wrote:
Oh gawd no! Not the crazy frog! Arghhhhhh! $&^%&*^%*&#$^ %$(%^((% %$(O*%(#*%^ %)*#$&%)*@#% %_(#&%*%) frog


Sun Sep 05, 2010 3:07 pm 
 Profile E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Karma functions powered by Karma MOD © 2007, 2009 m157y