Results 1 to 3 of 3
  1. #1
    vasavi.singh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default Displaying array elements as null except the last index

    Hi,

    I wrote a program in which I am storing the data entered in a text field in an array. While displaying the entire array it is displaying the correct values for the last index only and for other indexes it displaying null value

    <code>
    public void displayAuthorInformation(String name,int pub,String org,String dept,int autCount)
    {
    authorName=new String[autCount];
    publications=new int[autCount];
    organization=new String[autCount];
    department=new String[autCount];
    ------------
    ------------
    ----------
    count++;
    if(i<autCount)
    {
    authorName[i]=name;
    publications[i]=pub;
    organization[i]=org;
    department[i]=dept;
    System.out.println(authorName[i]+","+publications[i]+","+organization[i]+","+department[i]);
    i++;
    System.out.println("i: "+i);
    }
    if(i==autCount)
    {
    for(int j=0;j<i;j++)
    System.out.println(authorName[i]);
    System.out.println(publications[i]);
    System.out.println(organization[i]);
    System.out.println(department[i]);
    }
    }
    </code>



    This is the code what I did

    I was struggling since 1hr failed to find the error.

    can u people find the error to solve it


    regards,

    vasavi

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Are you aware that i only changes once when executing this code? There doesn't appear to be any kind of loop, which is probably what you were trying to do.

    Also, code tags are [CODE]

  3. #3
    vasavi.singh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default Displaying null values for the array

    Ya. I observed the point what you said.
    Actually the matter is that authorInterface window has to be opened for a specified number of times based on the input field entered in the previous window. for that when the user enter the information and validates it then only that authorInterface has to open. Otherwise no need. When user conforms that the information is correct then that information is stored in the array for future use(purpose: it has to be restored in a file later). Now also I am not able to find the error. Now I am sending the complete code of it so pls once go through it and suggest.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class AuthorInterface extends JPanel
    {
            private JTextField field1=new JTextField(10);
            private JTextField field2=new JTextField(4);
            private JTextField field3=new JTextField(10);
            private JTextField field4=new JTextField(10);
    
            JFrame autInterface = new JFrame("Author Interface");
            Container content = autInterface.getContentPane();
    
            String name,org,dept;
            int pub;
    	static int count=0,i=0;
    	
    	String authorName[]=new String[10];
    	String organization[]=new String[10];
    	String department[]=new String[10];
    	int publications[]=new int[10];
    
            public AuthorInterface()
            {
                    super(new BorderLayout());
    
                    JLabel label1=new JLabel("Author Name: ");
                    JLabel label2=new JLabel("Publication: ");
                    JLabel label3=new JLabel("Working Organization: ");
                    JLabel label4=new JLabel("Department: ");
    
                    JPanel labelPanel=new JPanel(new GridLayout(5, 1));
                    JPanel fieldPanel=new JPanel(new GridLayout(5, 1));
                    JPanel buttonPanel=new JPanel(new GridLayout(1, 2));
    
                    add(labelPanel,BorderLayout.WEST);
                    add(fieldPanel,BorderLayout.CENTER);
    
                    labelPanel.add(label1);
                    fieldPanel.add(field1);
                    labelPanel.add(label2);
                    fieldPanel.add(field2);
                    labelPanel.add(label3);
                    fieldPanel.add(field3);
                    labelPanel.add(label4);
                    fieldPanel.add(field4);
            }
    
    	public void displayAuthorInformation(String name,int pub,String org,String dept,int autCount)
            {
    		JFrame f = new JFrame("New Author Information");
                    f.setSize(250, 250);
                    f.setLocation(300,200);
                    f.getContentPane().add(BorderLayout.CENTER, new JTextArea(10, 40));
            String msg="Author Name: "+name+"\nPublication: "+pub+"\n Organization: "+org+"\nDepartment: " +dept;
                    int res=JOptionPane.showConfirmDialog(null,msg,"New Author Information",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
                    if(res==JOptionPane.OK_OPTION)
                    {
    			count++;
    			if(i<autCount)
    			{
    				authorName[i]=name;
    				publications[i]=pub;
    				organization[i]=org;
    				department[i]=dept;
    //				System.out.println(authorName[i]+","+publications[i]+","+organization[i]+","+department[i]);
    				i++;
    //				System.out.println("i: "+i);
    			}
    		}
    
    		if(i==autCount)
    		{
    /*			for(int j=0;j<i;j++)
    				System.out.println(authorName[j]+","+publications[j]+","+organization[j]+","+department[j]);*/
    			NewArticleInformation input=new NewArticleInformation();
    	                input.readAuthorInput(autCount,authorName,publications,organization,department);
    
    			String msg1="Thank You for adding a new Article Information.";
    			JOptionPane.showConfirmDialog(null,msg1,"New Author Information",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
    			
    			AcademicsSocialNetworkInterface interface2=new AcademicsSocialNetworkInterface();
    //			interface2.displayNetworkInformation();
    			interface2.academicsSocialNetworkInterfaceCreation();
    		}
                    f.setVisible(false);
                    AuthorInterface interface2=new AuthorInterface();
                    interface2.authorInterfaceWindow(autCount);
    	}
    
            public void authorInterfaceWindow(final int autCount)
            {
    		if(count< autCount)
    		{
    			final AuthorInterface interface1=new AuthorInterface();
    	
            	        JButton submit = new JButton("Check");
                    	JButton clear = new JButton("Reset");
    
    	                submit.addActionListener(new ActionListener() {
            	                public void actionPerformed(ActionEvent e)
                    	        {
                            	        if("Check".equals(e.getActionCommand()))
                                    	{
                                            	String name=interface1.field1.getText();
    	                                        int pub=Integer.parseInt(interface1.field2.getText());
            	                                String org=interface1.field3.getText();
                    	                        String dept=interface1.field4.getText();
                            	        interface1.displayAuthorInformation(name,pub,org,dept,autCount);
                                    	}
                            	}
                    	});
    
    	                clear.addActionListener(new ActionListener() {
            	                public void actionPerformed(ActionEvent e)
                    	        {
                            	        if("Reset".equals(e.getActionCommand()))
                                    	{
                                            	interface1.field1.setText("");
    	                                        interface1.field2.setText("");
            	                                interface1.field3.setText("");
                    	                        interface1.field4.setText("");
                            	        }
    	                        }	
            	        });
    
                    	autInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	                content.add(interface1, BorderLayout.NORTH);
            	        JPanel p=new JPanel();
                    	p.add(submit);
    	                p.add(clear);
            	        content.add(p,BorderLayout.SOUTH);
                    	autInterface.pack();
    	                autInterface.setVisible(true);
    		}
            }
    }
    can u respond fast

    regards,
    vasavi

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 10:16 AM
  2. Array Index problems
    By ragnor2004 in forum New To Java
    Replies: 4
    Last Post: 03-26-2009, 07:53 PM
  3. Replies: 5
    Last Post: 10-07-2008, 05:10 PM
  4. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:30 PM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 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
  •