从数组中查找当前索引并打印下一条记录

时间:2015-11-09 09:25:16

标签: java arrays

我有一个程序,有5个Button。 Button用于查找数组中的第一个,最后一个,下一个和上一个记录。 我已经完成了第一个和最后一个记录,但是我有一个混乱的方法来查找数组的下一个和上一个记录。Output

public void fn(String s2)
{
    try
    {
        BufferedReader br=new BufferedReader(new FileReader("Student.txt"));
        String s=br.readLine();
        Book[] b=Book.StringToObj(s);

        if(s2.equals("Read Data"))
        {
            l5.setText(String.valueOf(b[0].id));
            l6.setText(String.valueOf(b[0].title));
            l7.setText(String.valueOf(b[0].price));
            l8.setText(String.valueOf(b[0].auth));
        }

        if(s2.equals("<<"))
        {
            if(l5.getText().equals(String.valueOf(b[0].id))&&l6.getText().equals(String.valueOf(b[0].title))&&l7.getText().equals(String.valueOf(b[0].price))&&l8.getText().equals(String.valueOf(b[0].auth)))
            {
                JOptionPane.showMessageDialog(this,"You are already at First Record");
            }
            else
            {
                    l5.setText(String.valueOf(b[0].id));
                    l6.setText(String.valueOf(b[0].title));
                    l7.setText(String.valueOf(b[0].price));
                    l8.setText(String.valueOf(b[0].auth));
            }

        }

        if(s2.equals(">>"))
        {
            if(l5.getText().equals(String.valueOf(b[b.length-1].id))&&l6.getText().equals(String.valueOf(b[b.length-1].title))&&l7.getText().equals(String.valueOf(b[b.length-1].price))&&l8.getText().equals(String.valueOf(b[b.length-1].auth)))
            {
                JOptionPane.showMessageDialog(this,"You are already at Last Record");
            }
            else
            {
                l5.setText(String.valueOf(b[b.length-1].id));
                l6.setText(String.valueOf(b[b.length-1].title));
                l7.setText(String.valueOf(b[b.length-1].price));
                l8.setText(String.valueOf(b[b.length-1].auth));
            }
        }

        if(s2.equals("<"))
        {


            if(l5.getText().equals(String.valueOf(b[0].id))&&l6.getText().equals(String.valueOf(b[0].title))&&l7.getText().equals(String.valueOf(b[0].price))&&l8.getText().equals(String.valueOf(b[0].auth)))
            {
                JOptionPane.showMessageDialog(this,"This is a First Record, Previous Record is Not Available");
            }
            else
            {
            }

        }

        if(s2.equals(">"))
        {

        }

    }
    catch(Exception e)
    {
    }   
}

当我点击“&gt;”按钮我想从Array中获取Next记录。 我该怎么办?

2 个答案:

答案 0 :(得分:3)

为什么不将当前页面存储在int之类的变量中,然后用它进行操作?

这样的事情:

int page = 0;
Book[] books;

public void init() 
{
    BufferedReader br = new BufferedReader(new FileReader("Student.txt"));
    String s = br.readLine();
    books = Book.StringToObj(s);
}

public void show()
{
    l5.setText(String.valueOf(books[page].id));
    l6.setText(String.valueOf(books[page].title));
    l7.setText(String.valueOf(books[page].price));
    l8.setText(String.valueOf(books[page].auth));
}

public void fn(String s2)
{
    try
    {
        if(s2.equals("Read Data"))
        {
            show();
        }

        if(s2.equals("<<"))
        {
            if (page == 0)
            {
                JOptionPane.showMessageDialog(this,"You are already at First Record");
            }
            else
            {
                page = 0;
                show();
            }
        }

        if(s2.equals(">>"))
        {
            if (page == books.length - 1)
            {
                JOptionPane.showMessageDialog(this,"You are already at Last Record");
            }
            else
            {
                page = books.length-1;
                show();
            }
        }

        if(s2.equals("<"))
        {
            if (page == 0)
            {
                JOptionPane.showMessageDialog(this,"This is a First Record, Previous Record is Not Available");
            }
            else
            {
                page--;
                show();
            }
        }

        if(s2.equals(">"))
        {
            if (page == books.length - 1)
            {
                JOptionPane.showMessageDialog(this,"You are already at Last Record");
            }
            else
            {
                page++;
                show();
            }
        }
    }
    catch(Exception e)
    {
    }   
}

请注意,我已将books移至全局范围,因为每次都无法再次读取文件。

答案 1 :(得分:1)

您需要设置一个全局变量。将此变量用作索引以从数组中获取值。最初这将是一个,显示数组中的第一个元素。当您单击最后一次更改变量值时。单击next或previous时,只需增加或减少变量的值,并在该位置显示数组元素。