Hello im currently having problems with the storing of String to an array im currently using a GUI output so it reads inputs from the textfield here's my code:

import java.util.*;
import javax.swing.*;

public class Contents
{
int listLength;
String []word;
String []meaning;
String []sentence;
String []classification;


public Contents(int l)
	{
	this.listLength=l;
	word=new String[listLength];
	meaning=new String[listLength];
	sentence=new String[listLength];
	classification=new String[listLength];
	}

public void ADD(String word1,String meaning1,String sentence1,String classification1)
{
int x=0;
	while(x<word.length&&x<meaning.length&&x<sentence.length&&x<classification.length)
	{
		if(word[x]==null&&meaning[x]==null&&sentence[x]==null&&classification[x]==null)
		{
		word[x]=word1;
		meaning[x]=meaning1;
		sentence[x]=sentence1;
		classification[x]=classification1;
		x=word.length+1;
		}
	x++;
	}


}


public void SEARCH(String word1)
{
int none=word.length,loc;

	for(loc = 0;loc < listLength;loc++)
	{
		if(word[loc]==word1)
		{
			JOptionPane.showMessageDialog(null,"Search Result","Word:\n "+word[loc]+"\n\n Meaning:\n "+meaning[loc]+"\n\n Sentence:\n "+sentence[loc]+"\n\n Classfication:\n "+classification[loc]+" ",JOptionPane.INFORMATION_MESSAGE);
			none=none=1;
		}
	}

 	 if(none==word.length)
	 {

	 	JOptionPane.showMessageDialog(null,"Not Found!","This word is not found in this dictionary!",JOptionPane.ERROR_MESSAGE);
	 }

}

public void DISPLAY()
{
String a,b,c,d,e="";

for(int x=0;x<word.length;x++)
{
a=word[x];
b=meaning[x];
c=sentence[x];
d=classification[x];
e=""+a+" "+b+" "+c+" "+d+"";
}

	if(e.equals(""))
	JOptionPane.showMessageDialog(null,"EMPTY!","DISPLAY CONTENTS",JOptionPane.WARNING_MESSAGE);
	else
	JOptionPane.showMessageDialog(null,e,"DISPLAY CONTENTS",JOptionPane.INFORMATION_MESSAGE);

}










}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.UIManager.LookAndFeelInfo;



class Dictionary extends JFrame implements ActionListener
{
static DLL list = new DLL(null, null);
static DLL words = new DLL(null, null);
static Contents r=new Contents(100);
JFrame frame;
JTable table;
Vector rows,columns;
DefaultTableModel tabModel;
JScrollPane scrollPane;
JLabel name,product,date1,time,dot;
JTextField name1,product1,sentence;
JButton ADD,DELETE,DISPLAY,SEARCH,EDIT;
JComboBox classif;

public Dictionary()
{

setTitle("Dictionary");


setVisible(true);
Container c = getContentPane();
c.setLayout(null);
c.setSize(800,600);
Font font=new Font("Arial",Font.PLAIN,20);
Font font1=new Font("Arial",Font.PLAIN,30);

name=new JLabel("WORD");
product=new JLabel("MEANING");
date1=new JLabel("SENTENCE");
time=new JLabel("CLASSIFICATION");

dot=new JLabel(":");
dot.setFont(font1);
name.setFont(font);
product.setFont(font);
date1.setFont(font);
time.setFont(font);


name1=new JTextField();
product1=new JTextField();
name1.setFont(font1);
product1.setFont(font1);
sentence=new JTextField();
sentence.setFont(font1);

        classif = new JComboBox();
        classif.addItem("");
        classif.addItem("NOUN");
        classif.addItem("ADJECTIVE");
        classif.addItem("VERB");

name1.addKeyListener(new KeyAdapter(){
			public void keyTyped(KeyEvent e){
				char ch = e.getKeyChar();
				if(Character.isDigit(ch)){
					e.consume();
					}
			}
		});


product1.addKeyListener(new KeyAdapter(){
			public void keyTyped(KeyEvent e){
				char ch = e.getKeyChar();
				if(Character.isDigit(ch)){
					e.consume();
					}
			}
		});



ADD=new JButton("ADD");
DELETE=new JButton("DELETE");
DISPLAY=new JButton("DISPLAY");
SEARCH=new JButton("SEARCH");

/*JLabel name,product,date1,time;
JTextField name1,product1;
JButton ADD,DELETE;
JComboBox month,date,year,hour,minute,day; */

rows=new Vector();
columns= new Vector();
String[] columnNames =
{
"WORD",
"MEANING",
"SENTENCE",
"CLASSIFICATION",
};
addColumns(columnNames);

tabModel=new DefaultTableModel();
tabModel.setDataVector(rows,columns);

    table = new JTable(tabModel){
      public boolean isCellEditable(int rowIndex, int colIndex) {
        return false;   //Disallow the editing of any cell
      }
    };

table.getTableHeader().setReorderingAllowed(false);
scrollPane= new JScrollPane(table);//ScrollPane

table.setRowSelectionAllowed(true);



ADD.addActionListener(this);
DELETE.addActionListener(this);
DISPLAY.addActionListener(this);
SEARCH.addActionListener(this);
c.add(scrollPane);

c.add(name);
c.add(product); c.add(date1); c.add(time);
c.add(name1); c.add(product1);
c.add(ADD); c.add(DELETE);
c.add(sentence);
c.add(dot); c.add(DISPLAY);
c.add(classif);
c.add(SEARCH);

name.setBounds(10,5,200,50);
name1.setBounds(250,5,525,50);
product.setBounds(10,65,225,50);
product1.setBounds(250,65,525,50);
date1.setBounds(10,125,200,50);
sentence.setBounds(250,125,525,50);

time.setBounds(10,185,200,50);
classif.setBounds(250,185,525,50);

scrollPane.setBounds(10,245,760,305);
ADD.setBounds(10,560,180,100);
SEARCH.setBounds(200,560,180,100);
DELETE.setBounds(400,560,180,100);
DISPLAY.setBounds(590,560,180,100);
table.getParent();
DISPLAY.setFont(font1);
SEARCH.setFont(font1);
ADD.setFont(font1);
DELETE.setFont(font1);
// Disable auto resizing
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

// Set the first visible column to 100 pixels wide
int vColIndex = 0,vColIndex2 = 1,vColIndex3 = 2,vColIndex4=3;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
int width = 120;
col.setPreferredWidth(width);

TableColumn col2 = table.getColumnModel().getColumn(vColIndex2);
int width2 = 260;
col2.setPreferredWidth(width2);

TableColumn col3 = table.getColumnModel().getColumn(vColIndex3);
int width3 = 265;
col3.setPreferredWidth(width3);

TableColumn col4 = table.getColumnModel().getColumn(vColIndex4);
int width4 = 110;
col4.setPreferredWidth(width4);
}

 public void addColumns(String[] colName)//Table Columns
{
for(int i=0;i<colName.length;i++)
columns.addElement((String) colName[i]);
}




void deleteRow(int index)
   {
     if(index!=-1)//At least one Row in Table
      {
        rows.removeElementAt(index);
        table.addNotify();
       }

   }//Delete Row


	public void actionPerformed(ActionEvent e)
	{
		String a,b,c,classi,dat,ye,ho,min,da,total,wa="",we="";

	try
	{
	if(e.getSource()==ADD)
	{
		if(name1.getText().equals(we)||product1.getText().equals(wa)||sentence.getText().equals(wa)||classif.getSelectedItem()=="")
		{
					 String msg="Please fill up everything";
	              JOptionPane.showMessageDialog(null,msg,"WARNING!",JOptionPane.WARNING_MESSAGE);
		classif.setSelectedItem("");


		name1.setText("");
		product1.setText("");
		sentence.setText("");
		}

		else
		{



				a=name1.getText();
				b=product1.getText();
				c=sentence.getText();
				classi=classif.getSelectedItem().toString();
				total=a+" "+b+" "+c+" "+classi;
				Vector t = new Vector();
				t.addElement((String) ""+a);
				t.addElement((String) ""+b);
				t.addElement((String) ""+c);
				t.addElement(classif.getSelectedItem());

				rows.addElement(t);
				table.addNotify();

				//add to DLL
				list.addToTail(total);
				words.addToTail(a);

				//add to Contents
				r.ADD(a,b,c,classi);

				System.out.println(" "+total+" ");
				classif.setSelectedItem("");


				name1.setText("");
				product1.setText("");
				sentence.setText("");

		}
	}

	else if(e.getSource()==DELETE)
	{

		Object obj1 = GetData(table, table.getSelectedRow(), 0);
		Object obj2 = GetData(table, table.getSelectedRow(), 1);
		Object obj3 = GetData(table, table.getSelectedRow(), 2);
		Object obj4 = GetData(table, table.getSelectedRow(), 3);
		System.out.println(" "+obj1+"'s data has been deleted");

		String j,k,l,m,t;
		j=obj1.toString();
		k=obj2.toString();
		l=obj3.toString();
		m=obj4.toString();
		t=j+" "+k+" "+l+" "+m;


			list.delete(t);
			deleteRow(table.getSelectedRow());





	}

	else if(e.getSource()==SEARCH)
	{
	String inputValue =JOptionPane.showInputDialog("Enter a word you want to search");

	r.SEARCH(inputValue);


    }


	else if(e.getSource()==DISPLAY)
	{
	list.print();
	r.DISPLAY();
	}

	}

		catch(Exception z)
		{
			 String msg="INVALID ACTION!";
	              JOptionPane.showMessageDialog(null,msg,"ERROR!",JOptionPane.ERROR_MESSAGE);
		}
	}






public static void main(String[]args)
{
try{

for(LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
{
if("Nimbus".equals(info.getName()))
{
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
Dictionary o=new Dictionary();
o.setSize(800,700);
o.setResizable(false);
o.setLocationRelativeTo(null);

}

catch(Exception huhu)
{
System.out.println(huhu);
}
}

public Object GetData(JTable table, int row_index, int col_index){
    return table.getModel().getValueAt(row_index,col_index);
  }

}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.UIManager.LookAndFeelInfo;

class DLL{
	DLLNode head;
	DLLNode tail;

	public DLL(DLLNode head, DLLNode tail){
		this.head = head;
		this.tail = tail;
	}


	public void print()
	{
	DLLNode temp;

	String a,c="";

	for(temp = head;temp != null;temp=temp.next)
	{
	a=temp.item;

	c=""+c+"\n"+a+"";
	}

	if(c.equals(""))
	JOptionPane.showMessageDialog(null,"EMPTY!","DISPLAY CONTENTS",JOptionPane.WARNING_MESSAGE);
	else
	JOptionPane.showMessageDialog(null,c,"DISPLAY CONTENTS",JOptionPane.INFORMATION_MESSAGE);

	}




	public boolean isEmpty (){
		return(head==null);
	}


	public void addToTail(String j){
	if(! isEmpty()){
	tail.next = new DLLNode(j, null, tail);
	tail = tail.next;
	}
	else
		head = tail = new DLLNode(j, null, null);
	}


	public void delete(String item)	{

		DLLNode current = head;
		boolean val = false;
		DLLNode temp;


		 while (current != null && !val) {
					 temp = current;


			        if (current.item.equals(item)) {

						if(current.previous ==null)	{
									if(head.next==null)
									head = head.next;

									else	{
									head = head.next;
									head.previous = null;
									}

						}

						else if(current.next==null)	{
								temp = current.previous.previous;
								tail = current.previous;
								tail.next = null;
								tail.previous = temp;

								val = true;
								break;
						}

						else{
			            temp = current.previous.previous;
			           	current = current.previous;
			            current.next = current.next.next;
			            current.previous = temp;
			            current.next.previous = current;

			            val = true;
						break;
	       	 				}
						}
	       	 		current = current.next;
		}
	}

public String search (String item)
{
DLLNode current = head;
 while(head!=null)

 {

 if(head.equals(item)) // if the values match,
	{
 	JOptionPane.showMessageDialog(null,"RESULT"," "+head.item+" ",JOptionPane.INFORMATION_MESSAGE);
	}
 else
	{
 	head=head.next; // otherwise, move on
 	JOptionPane.showMessageDialog(null,"RESULT"," "+head.item+" ",JOptionPane.INFORMATION_MESSAGE);
	}
 }

 return null;

}




}
class DLLNode{
	public String item;
	public DLLNode next ;
	public DLLNode previous;


	public DLLNode(String item,DLLNode next, DLLNode previous){
		this.item = item;
		this.next=next;
		this.previous =previous;
	}

}

my only problem is within the Contents class because my inputted Strings could not be stored to the array.

These were the codes from the Dicionary class which connects to the Contents class

static Contents r=new Contents(100);
a=name1.getText();
b=product1.getText();
c=sentence.getText();
classi=classif.getSelectedItem().toString();
r.ADD(a,b,c,classi);

the 4 inputs should be stored inside the array. the code below was taken from the Contents class

public void ADD(String word1,String meaning1,String sentence1,String classification1)
{
int x=0;
	while(x<word.length&&x<meaning.length&&x<sentence.length&&x<classification.length)
	{
		if(word[x]==null&&meaning[x]==null&&sentence[x]==null&&classification[x]==null)
		{
		word[x]=word1;
		meaning[x]=meaning1;
		sentence[x]=sentence1;
		classification[x]=classification1;
		x=word.length+1;
		}
	x++;
	}


}

but when i display them it always says null. im really confused i need help.

I doubt that anyone will try to understand those hundreds of lines of incorrectly indented uncommented code, so you'll have to debug this yourself. Just use lots of print statements to confirm the values of all the variables in and around where things are going wrong. And don't forget to catch and printStackTrace every exception.

Just a few remarks:

1. in your contents class, in your search method, you are using the '==' operator to compare String objects. Don't do this, use the .equals method.
2. you having a bunch of unused variables and imports, might be better to leave them out.
3. that's .. quite a constructor in you Dictionary class. no way you can reduce the code in there? :)
4. as far as I can tell, your class works
5. In your dictionary class, you catch 'Exception z'. try printing the stacktrace here, even though it won't do you much good. You're better of using the specific Exceptions instead of the base - Catch-everything-here - Exception class.
6. in your search method .. what are you trying to do with 'none=none=1;'
7. In you dll class, you're trying 'head.equals(item)' while head is of type DLLNode, while item is a String instance.

in your search method: you're testing for the length of the lists with 'listLength', while you should use your variable 'none'

You may want to go over youre code again, and try to filter out these problems first.
also, why do you need several 'search, add, print, ...' methods?

Thanks for the replies. i have compressed my codes into a smaller one disregarding the DLL coz my only problem is about the String not being stored to the array.

here's my code:

ive also added an option in the beginning that lets the user input his desired array length.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.UIManager.LookAndFeelInfo;

class Test extends JFrame implements ActionListener
{
static String a=JOptionPane.showInputDialog(null,"ENTER LENGTH");
static int length=Integer.parseInt(a);
static Contents r=new Contents(length);
JFrame frame;
JLabel first,second,third,fourth;
JTextField one,two,three,four;
JButton ADD,DISPLAY;

public Test()
{

setTitle("Sample Output");
setVisible(true);
Container c = getContentPane();
c.setLayout(null);
c.setSize(500,300);
Font font1=new Font("Arial",Font.PLAIN,30);

first=new JLabel("First");
second=new JLabel("Second");
third=new JLabel("Third");
fourth=new JLabel("Fourth");

first.setFont(font1);
second.setFont(font1);
third.setFont(font1);
fourth.setFont(font1);

one=new JTextField();
two=new JTextField();
three=new JTextField();
four=new JTextField();

one.setFont(font1);
two.setFont(font1);
three.setFont(font1);
four.setFont(font1);

ADD=new JButton("ADD");
DISPLAY=new JButton("DISPLAY");


ADD.addActionListener(this);
DISPLAY.addActionListener(this);
DISPLAY.setFont(font1);
ADD.setFont(font1);


c.add(first); c.add(one);
c.add(second); c.add(two);
c.add(third); c.add(three);
c.add(fourth); c.add(four);
c.add(ADD); c.add(DISPLAY);

first.setBounds(10,5,200,50);
one.setBounds(250,5,525,50);
second.setBounds(10,65,225,50);
two.setBounds(250,65,525,50);
third.setBounds(10,125,200,50);
three.setBounds(250,125,525,50);
fourth.setBounds(10,185,200,50);
four.setBounds(250,185,525,50);

ADD.setBounds(10,260,180,100);
DISPLAY.setBounds(590,260,180,100);
}

	public void actionPerformed(ActionEvent e)
	{
	String a,b,c,d,wa="";
	try
	{

		if(e.getSource()==ADD)
		{
			if(one.getText().equals(wa)||two.getText().equals(wa)||three.getText().equals(wa)||four.getText().equals(wa))
			{
						 String msg="Please fill up everything";
		              JOptionPane.showMessageDialog(null,msg,"WARNING!",JOptionPane.WARNING_MESSAGE);

			one.setText("");
			two.setText("");
			three.setText("");
			four.setText("");
			}

			else
			{



					a=one.getText();
					b=two.getText();
					c=three.getText();
					d=four.getText();

					//add to Contents
				        r.ADD(a,b,c,d);

					one.setText("");
					two.setText("");
					three.setText("");
					four.setText("");

			}
		}

		else if(e.getSource()==DISPLAY)
			{
			r.DISPLAY();
			}

	}

			catch(Exception z)
			{
				 String msg="INVALID ACTION!";
		              JOptionPane.showMessageDialog(null,msg,"ERROR!",JOptionPane.ERROR_MESSAGE);
			}
	}


public static void main(String[]args)
{
try{

for(LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
{
if("Nimbus".equals(info.getName()))
{
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
Test o=new Test();
o.setSize(800,400);
o.setResizable(false);
o.setLocationRelativeTo(null);

}

catch(Exception huhu)
{
System.out.println(huhu);
}
}

public Object GetData(JTable table, int row_index, int col_index){
    return table.getModel().getValueAt(row_index,col_index);
  }

}
import java.util.*;
import javax.swing.*;

public class Contents
{
int listLength;
String []word;
String []meaning;
String []sentence;
String []classification;


public Contents(int l)
	{
	this.listLength=l;
	word=new String[listLength];
	meaning=new String[listLength];
	sentence=new String[listLength];
	classification=new String[listLength];
	}

public void ADD(String word1,String meaning1,String sentence1,String classification1)
{
int x=0;
	while(x<word.length&&x<meaning.length&&x<sentence.length&&x<classification.length)
	{
		if(word[x].equals(null)&&meaning[x].equals(null)&&sentence[x].equals(null)&&classification[x].equals(null))
		{
		word[x]=word1;
		meaning[x]=meaning1;
		sentence[x]=sentence1;
		classification[x]=classification1;
		x=word.length+1;
		}
	x++;
	}


}


public void DISPLAY()
{
String a,b,c,d,e="";

for(int x=0;x<word.length;x++)
{
a=word[x];
b=meaning[x];
c=sentence[x];
d=classification[x];
e=""+a+" "+b+" "+c+" "+d+"";
}

	if(e.equals(""))
	JOptionPane.showMessageDialog(null,"EMPTY!","DISPLAY CONTENTS",JOptionPane.WARNING_MESSAGE);
	else
	JOptionPane.showMessageDialog(null,e,"DISPLAY CONTENTS",JOptionPane.INFORMATION_MESSAGE);

}


}

i really need help. the process of my program is that the user inputs 4 times.
and each of them will be stored in 4 different arrays. but it seems like it cant be stored im still confused.

-----------------------------------

im guessing that this part is the main culprit of the problem.
this is my first time making a constructor with 4 arrays,but its really needed for my program.

int listLength;
String []word;
String []meaning;
String []sentence;
String []classification;


public Contents(int l)
	{
	this.listLength=l;
	word=new String[listLength];
	meaning=new String[listLength];
	sentence=new String[listLength];
	classification=new String[listLength];
	}

why using 4 different arrays? Why don't you create a class Word with 4 instance variables and store that either into one array, or List, might even be better, unless you want to limit the number of words that can be stored.

if you override the equals method for this class, you'll find it a bit easier to deal with, IMHO

coz i will be using the 4 arrays in my search method.

coz my search algorithm searches for an item in the first array and its index would be the basis for the other 3 arrays.

sample would be

if(item==array1[0])
{
item1=array1[0]
item2=array2[0]
item3=array3[0]
item4=array4[0]
search result = item1 item2 item3 item4
}

the concept would be like a dictionary search engine. the user searches for the word.
and the program display the word,sentence,meaning and its classification

ehm ... do you understand my post?
you would get all the same information without having to extract from 4 different arrays. yes, you would have to put "some effort" in creating the original Word class, but you would have to do less effort on all the rest.

besides: item==array1[0] would not work, not in the approach I suggested, but also not in your approach. as I stated earlier: use the .equals method to compare objects, not the == operator.

my only problem is about storing my String input into the array

now i tried making it into only one array and adding only one input but it is still not stored to the array.

i have shortened the code from my class into this:

int listLength;
String []word;



public Contents(int l)
	{
	this.listLength=l;
	word=new String[listLength];

	}

public void ADD(String word1)
{
int x=0;
	while(x<word.length)
	{
		if(word[x].equals(null))
		{
		word[x]=word1;
		x=word.length+1;
		}
	x++;
	}


}

and in my main class into this

static String a=JOptionPane.showInputDialog(null,"ENTER LENGTH");
static int length=Integer.parseInt(a);
static Contents word1=new Contents(length);
String a;
a=one.getText(); // <-- Input from the textfield
word1.ADD(a);    // <-- adding the input to the array

Storing the String into the array is really my big problem so that i could finish my program :(
its the last piece of the puzzle.

apparently not .. :p
try de-bugging your ADD method:
enter some printlines in there, check the values of the variables you're using there and see if they are what you expect them to be. (just to get started, this will propably not solve every problem you have)

before ive tried passing inputs from textfield into an int array and it worked well.

but now when it became a string array the input from textfield cant be stored into the array anymore.

got any simple coding tips on how can i fix it :( im out of ideas what to do next. im completely blanked out now.

if it worked for ints, it should have worked for your objects as well ... maybe it's your ( if element == null replace element ) approach that holds you back. if you want to use a normal array, you should have the correct index where you want the element added, otherwise, I would use a list and just use the add method to add the new information.

if(word[x].equals(null))

This code is broken. if word[x] is null then word[x].equals(anything) will throw a null pointer exception. Either the code isn't being executed, or array is initialised to something other than nulls, or you are ignoring the exception.

dude thanks alot it was just within the codes!

im done! god bless you! thanks for the time!

i made it into word[x]==null.

ive figured out a null isnt a string at all so it cant be .equals(null). instead ==null. cheers everyone. my problem is solved.

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.