Java: Cannot print the value of textbox in loop - java

i have 3 textboxes. One textbox contains the some integer, eg:2 What i want to do is i want to iterate the loop 2 times.. in every iteration i want to print the value in textboxes. I am clearing the textbox before going to the next iteration. After 2 iterations i want to make the textboxes editable false. But the problem is the loop iterates 2 times but the value in textbox is printed only for the 1st iteration. I wrote the following code
int i=1;
do
{
System.out.println(ext_people_name.getText());
System.out.println(ext_contact_num.getText());
System.out.println(i);
ext_people_name.setText("");
ext_contact_num.setText("");
i++;
if(i>nop1)
{
ext_people_name.setEditable(false);
ext_contact_num.setEditable(false);
break;
}
}while(i<=nop1);
The output i get right now is
neha
8798
1
2
Can anyone tell any alternative to this or please help

I am clearing the textbox before going to the next iteration
There, you solved your own problem!

Its better to use for loop instead of do/while for your code. Refactor your code like this:
for (int i=1; i<=nop1; i++) {
System.out.println(ext_people_name.getText());
System.out.println(ext_contact_num.getText());
System.out.println(i);
ext_people_name.setText("");
ext_contact_num.setText("");
};
ext_people_name.setEditable(false);
ext_contact_num.setEditable(false);

You gave the answer yourself - you CLEAR the textbox before the next iteration. So, it does not contain a value, so it won't print a value.
What you need to do, is to redesign what you are doing and break it into smaller steps. Instead of trying to use one loop to do everything, break it down into smaller loops.
I also do not quite understand what you want to do precisely. Why is the textboxes cleared before the next iteration?
If you only want to display the textbox values twice, and then clear them and make them uneditable, do that in separate steps.
Alternatively, edit your original question and add some more context that we can understand it better.

You are clearing the value in the textbox after each iteration, this make the value empty and prints nothing
System.out.println("some "+ext_people_name.getText());
System.out.println("some "+ext_contact_num.getText());
use above code that, you can come across that you are overwirting the result after iteration

Related

Is there a better way to access the last element in a for loop in Java

[edited the post to be more clear]
The code below is supposed to represent a number of trial runs, and within each trial assume a is the number of cases that need to be checked.
while(testCaseAmt > 0) {
for(int i = 0; i < a; i++) {
if(some condition)
//code when i is not the last element
//then I would like to break/return here.
if(i = a - 1)
//code when i is the last element
//then if the above if statement never runs during the a amt of trials
//i will print out something else
}
testCaseAmt--;
}
My question is if there is another way to access specifically the last element within a for loop to give it specific instructions. I want to loop through a set trials and if they meet the first condition, I stop the loop immediately and return. However, if the condition isn't found I still need to print out not found.
Cheers.
To me this seems related to "off-by-one errors" or "fencepost errors", see here.
If the code you're executing when i is not the last element isn't too complex, it could be better to just end the for loop one iteration earlier and write the specific code for the last element outside the for loop.
Adding an if statement inside a for loop where you know it will only be true at the very last iteration could be seen as bad-style.
You can define a boolean conditionMet = false before the for-loop and iterate until i < a-1.
If some condition applies, execute your code, then set conditionMet = true and break out of the for-loop.
After the for-loop, check if !conditionMet and in this case, execute the code for the last element.

How to determine when an action should be done after several loops

So basically, I have a programming assignment, and it involves two lines at an airline, one for frequent flyers and one for regular flyers. I have a for loop set up that, each time it runs, it determines whether some amount of frequent flyers gets in, some amount of regular flyers, or no one. For a frequent flyer, it is supposed to take 3 minutes to service a flyer, with one "minute" being a single iteration of the loop. For the regular flyers, it will take 5 minutes to service one person, again, with one iteration of the loop being a single minute. How can I determine when someone in the line has been serviced? As in, how can I pull someone out of the line whenever 3 or 5 iterations of the loop have occured, respectively. I'm utilizing a linked list.
I'm not sure I entirely understand your question, but I think I got the idea.
You could use break; to exit the loop (if that's what you're trying to do), and to test the amount of times the loop has been executed, you can use an int. It will look something like this:
int i = 0;
for(initialization, condition, iteration){
//Your code
if(i == 3 | i == 5){
break; //If you have your own method to "pull someone out of the line",
//then insert it here instead of using break, and also insert i = 0.
}
i++;
}

Printing the array

I am trying to print the contents of an array. For example I have defined an int array of size 10. But the user entered only 8 numbers. So the last two positions in an array have an value of zero. When I am printing the array I get all ten positions. Is it possible to print only up till the user entered.Also the user decides how many to enter so I cannot hard code the position for printing the array. Thanks.
Is it possible to print only up till the user entered.
Yes, keep track of how many items the user entered. This can be done with a separate int counter variable that you increment as the user enters an item, or if you want, you can fill the array with values that the user would never enter, for example, Integer.MIN_VALUE, and then display results up until you reach the non-sense values. The danger here is, what happens if the user just happens to pick that non-sense value? That's why I'd go with the first suggestion.
Also the user decides how many to enter so I cannot hard code the position for printing the array.
Yep, just as I said above.
Edit: or best of all, just do what Patricia Shanahan says.
If you have to use an array, keep track of the number of valid elements as suggested by Hovercraft Full Of Eels' answer.
If you have the option, it may be cleaner to use an ArrayList, which grows as you add elements. That way, at the end of input it will have exactly the right size, and you can print the whole of it.

Replacing value of a two dimensional java ArrayList

I have two dimensional arrayList, and I want to replace the that is set in the program. The problem is, if i replace the value of arrayList by "ONE", it will work, if I replace it by another value bigger than one, the program enters into a loop. I am using the following syntax:
arrayList.get(index).set(index2,VALUE)
Basically this shouldn't be happening, cause I am not changing anything within the loops, or?
My Questions is WHY? and How to fix it?
the code that is (i assume) producing the problems is:
if(mark.get(index1).get(index2) == 1 && mark.get(i-1).get(j) != 1){
// Replace the value
mark.get(i-1).set(j,1);
flag = true;
}
EDIT: I removed my code, cause it gave the impression I wanted help with the code, i had posted it so that you know what I was talking about. thanks
Got what the problem was! All the initials indexes had a value of ZERO, and I had a condition that was checking if it was not ONE(assuming it will be zero anyway). if i changed it to any other value than ONE then in the next repetitions the condition would have been voided.

Problem using if statement with ArrayList

I am using an if statement as shown below,
if(sign.size()==0)
Here sign is of the type ArrayList<Character>
I am trying to add a char to the ArrayList
But its not working. Is there anything wrong with my if statement?
I also tried the same with an ArrayList<doubler>, this time I could get into the if statement.
Is there anything wrong with the if statement?
I am having very difficult time understanding what you are saying, but it sounds like you are trying to figure out how to increase the list capacity. You do not need to manually do this. Simply add items using add() method and the list will re-size itself as appropriate.
Try if(sign.isEmpty()) instead. Also make sure you you're using good code style with {'s where needed. ArrayLists will adjust their own size to accommodate what you put in, so I'm not sure you even need this check.
You commented on an answer and said,
i want to get in the if only if i have 1 char in the array... but i does not get in to the if.... way if i have only one index
If you want to enter the IF only when there is one char, then you'd want
IF(sign.size() == 1){
...some code
}
Otherwise, if you want to enter the IF when size is 0, use isEmpty.
As you said
I want to get in the if only if I have
1 char in the array... but it does not
get in to the if.... what if i have
only one index?
If you are trying to enter the if statement only when there is only one char in the ArrayList then you can use
if(sign.size() == 1){
//your code
}
The condition will only be true when the size of the ArrayList is equal to 1.

Categories

Resources