Reputation: 1544
I have a program, there is 5 Button. The Button works to Find first, Last, Next and Previous Record from array.
I have done First and Last Record but i have a =n confusion to find next and Previous record from array.
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)
{
}
}
when i click on ">" Button i want Next record from Array. What i have to do?
Upvotes: 0
Views: 135
Reputation: 1575
You need to set a Global variable.Use this variable as index to get value from array. Initially this will be one, showing the first element in array. When you click last change the variable value also. When click next or previous just increase or decrease the value of variable and display the array element in that location.
Upvotes: 1
Reputation: 34199
Why don't you just store the current page in an variable like int
, and then manipulate with it?
Something like this:
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)
{
}
}
Note that I have moved books
to the global scope too, because there is no sense in reading the file again everytime.
Upvotes: 3