I have a simple fact app that has an array of different facts.
I have a next, previous, and home button.
When the home button is pressed, I want the first fact to be displayed again, and after, it will start incrementing again from the first array value.
My home button is not working. If I hit the next button 5 times, then hit the home button, I will be directed to the first fact, but if I hit the next button, then the 6th fact will display, not the second.
Here is my code:
public String nextFact() {
i++;
if(i >= facts.length) {
i = 0;
}
return facts[i];
}
public String previousFact() {
i--;
if(i < 0) {
i = facts.length - 1;
}
return facts[i];
}
public String homeButton() {
int i = 0;
return facts[i];
}
You are declaring a new local version of i.
It should be:
public String homeButton() {
i = 0;
return facts[i];
}
According to your code, I am assuming i is a variable shared across the three methods, which keeps track of the index of the question currently displayed. If that is the case, your method should reset the class member i to 0, instead of creating a local variable.
public String homeButton() {
i = 0;
return facts[i];
}
This should do the trick.
Related
I am creating a program that takes an input file, parses that information and builds a GUI calculator from that information. Currently the program works well except for when I implement an ActionListener on buttons that should set a text field to the value of the buttons getText() method.
I have tried a few different loop constructions using for and while, but all of them I have implemented have not found the where i or a counter is equal to the parsed int from numPad.getText() or returned 0 for all of the buttons.
The problem I am having while testing is that the variable i never matches numPoint. Logically my approach is to decrement i so that the loop will continue to look for matches, but never does this. The test output statements infinite loop "-1" for i and "7" for numPoint. As a note, the numPad array is not in order but instead the elements are as follows {7, 8, 9, 4, 5, 6, 1, 2, 3, 0}.
I realize that this loop may not be logically correct, but I am having a hard time finding a solution that works. I want to avoid hard coding if statements (such as i == Integer(parseInt.numPad[0].getText()) which would work.
This is the loop that creates the new buttons and adds them to an Array, sets the text based on a list created from the values of the input file and adds an ActionListener.
for (int i = 0; i < run.buttons.size(); i++) {
numPad[i] = new JButton();
numPad[i].setText(run.buttons.get(i));
numPad[i].addActionListener(new ButtonActionListener());
panel1.add(numPad[i]);
}
This is the most recent attempt at creating a loop that should make the assignment.
public static class ButtonActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
int i;
int numPoint;
for (i = 0; i < numPad.length; i++) {
numPoint = Integer.parseInt(numPad[i].getText());
if (i == numPoint) {
//Match, assign
System.out.println("works");
break;
} else {
//Decrement and continue
i--;
System.out.println("test statement" + i + " " + numPoint);
continue;
}
}
}
}
There are several ways you might do this, but let's start with the basics
for (int i = 0; i < run.buttons.size(); i++) {
numPad[i] = new JButton();
numPad[i].setText(run.buttons.get(i));
// You don't "have" to do this, as the action command defaults
// to the text of the button, but this is away to provide some
// kind of identifier to the action which might be
// different from the text
numPad[i].setActionCommand(run.buttons.get(i))
numPad[i].addActionListener(new ButtonActionListener());
panel1.add(numPad[i]);
}
Then in your ActionListener...
public static class ButtonActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
int numPoint = Integer.parseInt(command);
// Perform what ever action you need
}
Why is my assertion failing when I am trying to assert the amount of textviews in a list ?
#Test
public void testDeleteNote() throws Exception {
int count= getNoOfTextViews();
// Checking if count is greater than 0.
if (count > 0) {
// Iterating count times
for (int i = 0; i < count; i++)
{
// Checking if count is even or odd.
if (i % 2 == 0) {
solo.clickInList(0);
deleteNote();
} else {
// Clicking Long on the 1st item in the Notes List.
solo.clickLongInList(0);
// Clicking on Delete String.
solo.clickOnText(solo.getString(R.string.menu_delete));
}
}
count = getNoOfTextViews();
// Asserting all the text views are deleted.
assertEquals(0, count);
}
public int getNoOfTextViews() {
// Getting the count of text views in the activity.
ArrayList<TextView> textView = solo.getCurrentViews(TextView.class,
solo.getCurrentViews(ListView.class).get(0));
return textView.size();
}
The failure I am seeing is:
junit.framework.AssertionFailedError: expected:<0> but was:<1>
UPDATE:
I am seeing that this passes when i debugg, it fails only when i run the test case.
Your count variable is calculated once at the beginning of your program and then never updated. This means, when you are comparing it with 0 at the end, it still contains the old value.
All you have to do is update your count variable by calling the method again:
count = getNoOfTextViews();
Or simply
assertEquals(0, getNoOfTextViews());
Added a wait for list view before taking the count. This helped me solve the problem.
Thanks all.
public int getNoOfTextViews() {
solo.waitForView(ListView.class,0,1000);
// Getting the count of text views in the activity.
ArrayList<TextView> textView = solo.getCurrentViews(TextView.class,
solo.getCurrentViews(ListView.class).get(0));
return textView.size();
}
I am currently making a paint program and am implementing the undo and redo function. So far, if I remove the code for the redo part, it works, but for some reason, if I add the redo code back in, it just throws a arrayOutOfIndex exception or NullPointer. I have tried everything, and do not know what else there is to do!
Sorry if this problem is painstakingly easy; I am just a beginner.
Code:
public void redo()
{
for(int i = 0; i < 3; i++ ){
if(redo[0].get(0)!=null){
al.add((Shape) redo[0].get(redo[0].size()-1));
cl.add((Color) redo[1].get(redo[1].size()-1));
tl.add((Integer) redo[2].get(redo[2].size()-1));
lineEnd.add((Integer) redo[3].get(redo[3].size()-1));
junction.add((Component) redo[4].get(redo[4].size()-1));
}
redo[0].remove(redo[0].size()-1);
redo[1].remove(redo[1].size()-1);
redo[2].remove(redo[2].size()-1);
redo[3].remove(redo[3].size()-1);
redo[4].remove(redo[4].size()-1);
}
repaint();
}
public void undo()
{
for(int i = 0; i < 3; i++ ){
Shape alComponent = al.get(al.size()-1);
Color clComponent = cl.get(cl.size()-1);
Integer tlComponent = tl.get(tl.size()-1);
Integer lineEndComponent = lineEnd.get(lineEnd.size()-1);
Integer junctionComponent = juncture.get(juncture.size()-1);
if(al.get(0)!=null){
redo[0].add(alComponent);
redo[1].add(clComponent);
redo[2].add(tlComponent);
redo[3].add(lineEndComponent);
redo[4].add(junctionComponent);
}
al.remove(al.size()-1);
cl.remove(cl.size()-1);
tl.remove(tl.size()-1);
lineEnd.remove(lineEnd.size()-1);
juncture.remove(juncture.size()-1);
}
repaint();
}
P.S., I have a for loop in there because if you press it once, the change on the painting is minute.
Inside your redo loop, you
check that there is a redo-able item and redo if you can, then
(unconditionally) remove an item from the redo lists
Move the remove()s into the if block.
Also, don't use five arrays. Create a class that does all that for you, with well-named redo lists. (That's just a tip, has nothing to do with your question.)
I wanted the help of you fellow users of Java in making up my computer project. I want to add the loop command for the "if-else"method given in the code below . I am attaching a picture of the design for clearer understanding.
Objective:
To make a calculator which shows how many odd or even numbers I have entered.
Problem:
I am not sure what the loop method is like.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int odd = 0 ;
int even = 0 ;
int a = Integer.parseInt(txt1.getText());
if (a%2==0)
{
even++;
lbleven.setText(""+even);
}
else
{
odd++;
lblodd.setText(""+odd);
}
}
Every time you click on the button, even and odd will set to zero as they are local variables not instance variables, you have to make them instance variables so that they can hold the last value set to them.
private int odd;
private int even;
//....
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a = Integer.parseInt(txt1.getText());
if (a%2==0)
{
even++;
lbleven.setText(""+even);
}
else
{
odd++;
lblodd.setText(""+odd);
}
}
Or, you can get the odd&even labels text every time and increase them inside your condition.
int even= Integer.parseInt(lbleven.getText());
lbleven.setText(""+(++even))
I want to add an image to an ImageButton depending on a number between 0 and 10. My getNumber method is:
public int getNumber(){
// get a random number between 0 and 10
Random randomNumber = new Random();
num = randomNumber.nextInt(10);
return num;
}
I want every image to be unique but the problem I was having was that if numList did contain num it just would leave the button blank. I've tried to call permuteButton again recursively until num is not contained within my list but this does not seem to work.
public void permuteButton(ImageButton btn){
getNumber();
for(int i=0; i<=numList.size(); i++){
//check if the number is already being used
if( numList.contains(num) ){
permuteButton(btn);
}
// else the list doesnt have the number so assign the picture and add number to list
else{
numList.add(num);
assignPictures(btn);
}
}
}
Any help would be appreciated. I'm sorry if this is a simple question.
There are various things wrong with this code:
It would be better to have a single instance of Random instead of creating a new instance on each call to getNumber()
Rather than changing an instance variable within getNumber(), it would be sensible to just return the value and assign that to a local variable in permuteButton
Instead of recursion, you could use a while loop in permuteButton:
int num = getNumber();
while (numList.contains(num)) {
num = getNumber();
}
numList.add(num);
assignPictures(btn); // Presumably you'd now want to pass in num too
It would probably be a better idea to just shuffle the list to start with, create a Queue from it, then you can just take an item from the queue each time you need one. (This would also make it very easy to spot when you've used them all)
My answer is similar to the last suggestion from Jon Skeet.
// might be more than 10 ImageButtons, with only 10 images
for (ImageButton imageButton : imageButtons)
imageButton.putImage(randomImage.next());
...
public class RandomImage {
private final List<Image> shuffledImages;
private int currentIndex;
public RandomImage(List<Image> images) {
shuffledImages = new ArrayList<>(images.size());
shuffledImages.addAll(images);
currentIndex = -1;
}
public Image next() {
currentIndex++;
if (currentIndex % shuffledImages.size() == 0) {
currentIndex = 0;
Collections.shuffle(shuffledImages);
}
return shuffledImages[currentIndex];
}
}