Hi guys, I need some help.

Im trying to create a comment system in an applet.
When a user enters a new comment all previous comments are shown below.

The comments are taken from two text fields.

At the moment im just storing the latest 2 comments in variables, I need to find a way of adding them to an array.
All my efforts have failed as I cant seem to find a way to add an element to the very end of an array.

Ive pasted both the comment system and a stripped down version of the applet, just so you can replicate what im looking at right now.

My current comment system is below.

if(comment1 == null)
{	
	comment1=commentField.getText() + ":" + searchField.getText();
    resultMessage = comment1;
}
else
{
	comment2 = comment1;
	comment1=commentField.getText() + ":" + searchField.getText();
	resultMessage = comment1 + "     |     " + comment2;
}

This is a stripped down version of my applet

import java.util.*;
import java.applet.Applet ;
import java.awt.*; // import the java . awt package
import java.awt.event.*; // import the java . awt . e v ent package
import java.awt.*;

public class applet extends Applet implements ActionListener, MouseListener 
{
	int ovalxcord,ovalycord,ovalsize=20;
	int x, y, width, height;
	String resultMessage = "";
	int messageX = 115, messageY = 50;
	String comment[] = new String [10];
	String location[] = new String [10];
	int commentno;
	
	
    public void paint(Graphics g)
    //pre: g is valid graphics object
    //post: draws to Applet window
    {
        setSize(800,300);
        
        // draw search
        Font standardBold = new Font("Arial", Font.BOLD, 12);
        g.setFont(standardBold);
        g.drawString ("Search:", 70,20);
        Font standard = new Font("Arial", Font.PLAIN, 12);
        g.setFont(standard);
        g.drawString(resultMessage,messageX,messageY);
        
        // draw black circle
        g.setColor(Color.black);
		g.fillOval(ovalxcord-(ovalsize/2),ovalycord-(ovalsize/2),ovalsize,ovalsize);
        
    }
    
    public void mouseClicked( MouseEvent e)
    {
 	   ovalxcord=e.getX();
 	   ovalycord=e.getY();
 	   repaint();
    }
   
    public void mousePressed(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}

    public void mouseReleased(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
   	
   	Button searchButton;
	Button infoButton;
	TextField searchField;
	TextField commentField;
	Button commentButton;
   	
   	public void init()
	{
	   	searchButton = new Button("Search");
	   	infoButton = new Button("Information");
	   	commentButton = new Button("Add Comment");
		searchField = new TextField(20);
		commentField = new TextField(20);
		add(searchField);
		add(searchButton);
		add(infoButton);
		add(commentField);
		add(commentButton);
	
		searchButton.addActionListener(this);
		infoButton.addActionListener(this);
		commentButton.addActionListener(this);
	}
   	
   	public void start( )
   	{
   		addMouseListener(this);
   	}
    
   	String comment1, comment2;
   	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == searchButton)
        {
			resultMessage = searchField.getText();
			messageX = 115; messageY = 50;
    	}
        else
        if(e.getSource() == infoButton)
    	{
        	resultMessage = "Information " + ovalxcord + "," + ovalycord;
        	messageX = ovalxcord+ovalsize;
        	messageY = ovalycord+ovalsize/4;
    	}
		
		if(e.getSource() == commentButton)
    	{
        	if(comment1 == null)
        	{	
        		comment1=commentField.getText() + ":" + searchField.getText();
        		resultMessage = comment1;
    		}
        	else
        	{
        		comment2 = comment1;
        		comment1=commentField.getText() + ":" + searchField.getText();
        		resultMessage = comment1 + "     |     " + comment2;
        	}
        	
    	}
        repaint();
	}
        
}

Any help is appreciated.

Thanks

Hi guys, I need some help.

Im trying to create a comment system in an applet.
When a user enters a new comment all previous comments are shown below.

The comments are taken from two text fields.

At the moment im just storing the latest 2 comments in variables, I need to find a way of adding them to an array.
All my efforts have failed as I cant seem to find a way to add an element to the very end of an array.

Ive pasted both the comment system and a stripped down version of the applet, just so you can replicate what im looking at right now.

My current comment system is below.

if(comment1 == null)
{	
	comment1=commentField.getText() + ":" + searchField.getText();
    resultMessage = comment1;
}
else
{
	comment2 = comment1;
	comment1=commentField.getText() + ":" + searchField.getText();
	resultMessage = comment1 + "     |     " + comment2;
}

This is a stripped down version of my applet

import java.util.*;
import java.applet.Applet ;
import java.awt.*; // import the java . awt package
import java.awt.event.*; // import the java . awt . e v ent package
import java.awt.*;

public class applet extends Applet implements ActionListener, MouseListener 
{
	int ovalxcord,ovalycord,ovalsize=20;
	int x, y, width, height;
	String resultMessage = "";
	int messageX = 115, messageY = 50;
	String comment[] = new String [10];
	String location[] = new String [10];
	int commentno;
	
	
    public void paint(Graphics g)
    //pre: g is valid graphics object
    //post: draws to Applet window
    {
        setSize(800,300);
        
        // draw search
        Font standardBold = new Font("Arial", Font.BOLD, 12);
        g.setFont(standardBold);
        g.drawString ("Search:", 70,20);
        Font standard = new Font("Arial", Font.PLAIN, 12);
        g.setFont(standard);
        g.drawString(resultMessage,messageX,messageY);
        
        // draw black circle
        g.setColor(Color.black);
		g.fillOval(ovalxcord-(ovalsize/2),ovalycord-(ovalsize/2),ovalsize,ovalsize);
        
    }
    
    public void mouseClicked( MouseEvent e)
    {
 	   ovalxcord=e.getX();
 	   ovalycord=e.getY();
 	   repaint();
    }
   
    public void mousePressed(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}

    public void mouseReleased(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
   	
   	Button searchButton;
	Button infoButton;
	TextField searchField;
	TextField commentField;
	Button commentButton;
   	
   	public void init()
	{
	   	searchButton = new Button("Search");
	   	infoButton = new Button("Information");
	   	commentButton = new Button("Add Comment");
		searchField = new TextField(20);
		commentField = new TextField(20);
		add(searchField);
		add(searchButton);
		add(infoButton);
		add(commentField);
		add(commentButton);
	
		searchButton.addActionListener(this);
		infoButton.addActionListener(this);
		commentButton.addActionListener(this);
	}
   	
   	public void start( )
   	{
   		addMouseListener(this);
   	}
    
   	String comment1, comment2;
   	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == searchButton)
        {
			resultMessage = searchField.getText();
			messageX = 115; messageY = 50;
    	}
        else
        if(e.getSource() == infoButton)
    	{
        	resultMessage = "Information " + ovalxcord + "," + ovalycord;
        	messageX = ovalxcord+ovalsize;
        	messageY = ovalycord+ovalsize/4;
    	}
		
		if(e.getSource() == commentButton)
    	{
        	if(comment1 == null)
        	{	
        		comment1=commentField.getText() + ":" + searchField.getText();
        		resultMessage = comment1;
    		}
        	else
        	{
        		comment2 = comment1;
        		comment1=commentField.getText() + ":" + searchField.getText();
        		resultMessage = comment1 + "     |     " + comment2;
        	}
        	
    	}
        repaint();
	}
        
}

Any help is appreciated.

Thanks

to add a value to an array we use:

String comment[]=new String[10];
comment[0]=value;//0 is the first index of the array

if you want to add a value starting from the end of the array you would use a for loop to iterate through the array in reverse:

for(int i=comment.length-1;i>=0;i--) {
if(comment[i]==null){//to skip already filled array index
//add value to array
}
}

Thanks for the prompt reply.
Ive created this just to experiment with the code you gave me.

public class arrays {
	public static void main( String args [] )
	{
	    String comment[]=new String[10];
	    comment[0]="comment0";
	    comment[1]="comment1";
	    
	    for(int i=comment.length-1;i>=0;i--) 
	    {
		    if(comment[i]==null)
		    {
		    	comment[i]="comment2";
		    }
	    }
	    
	    for(int index = 0; index<3; index++)
	    System.out.println(comment[index]);
	}
}

That seems to work, is there any way to use the second example you gave me, but display higher value elements first, so to display the most recent comments before the older ones?

comment[2]
comment[1]
comment[0]

Thanks again

Sorry thats my mistake, give me two minutes to experiment.
Thanks

Thanks for the prompt reply.
Ive created this just to experiment with the code you gave me.

public class arrays {
	public static void main( String args [] )
	{
	    String comment[]=new String[10];
	    comment[0]="comment0";
	    comment[1]="comment1";
	    
	    for(int i=comment.length-1;i>=0;i--) 
	    {
		    if(comment[i]==null)
		    {
		    	comment[i]="comment2";
		    }
	    }
	    
	    for(int index = 0; index<3; index++)
	    System.out.println(comment[index]);
	}
}

That seems to work, is there any way to use the second example you gave me, but display higher value elements first, so to display the most recent comments before the older ones?

comment[2]
comment[1]
comment[0]

Thanks again

well to get the most recent comments you'd just iterate through the array from the beginning :

for(int i=0;i<comment.length;i++) {
if(comment[i]!=null){//to skip unfilled array values
//when we get here we will have the latest comment
break;//so we dont go through all the comments
}
}

Id like to beable to see all the comments, but the latest ones first.
So if the latest was comment2, and the first was comment0 id like to see comment2 at the top, then comment1 and finally comment0.

Thanks

Nevermind, lets go back to your 2nd example.

public class arrays {
	public static void main( String args [] )
	{
	    String comment[]=new String[10];
	    comment[0] = "comment0";
    	comment[1] = "comment1";
    	
        for(int i=comment.length-1;i>=0;i--) 
        {
            if(comment[i]==null)
            {
            	comment[i]="comment2";
            	break;
            }
            System.out.println(comment[i]);
    	}
	}
}

Do you know how I can assign a string to just one element and not the remaining elements in the array?

Thanks

Nevermind, lets go back to your 2nd example.

public class arrays {
	public static void main( String args [] )
	{
	    String comment[]=new String[10];
	    comment[0] = "comment0";
    	comment[1] = "comment1";
    	
        for(int i=comment.length-1;i>=0;i--) 
        {
            if(comment[i]==null)
            {
            	comment[i]="comment2";
            	break;
            }
            System.out.println(comment[i]);
    	}
	}
}

Do you know how I can assign a string to just one element and not the remaining elements in the array?

Thanks

To iterate through the entire array leave out the break statement.

Do you know how I can assign a string to just one element and not the remaining elements in the array?

well it depends, how would you know what element to assign this value too?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.