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.