I am Beginner practicing java with bluej and trying to print the next element every time I use the getNext() method . I tried some options but they didn't work and now I am stuck . Here is my code:
public void getNextQuestion()
{
int counter = 0;
Iterator<Questions> it = this.questions.iterator();
while(it.hasNext())
{
counter = counter + 1;
Questions nextObject = it.next();
System.out.println(counter+ ". " + nextObject.getDescription());
}
}
I am guessing you only want to print one question whenever getNextQuestion is called. In that case, you need to do this:
public class MyClass {
int counter = 0;
public void getNextQuestion()
{
Questions nextObject = questions.get(counter);
counter = counter + 1;
// If counter gets to the end of the list, start from the beginning.
if(counter >= questions.size())
counter = 0;
System.out.println(counter+ ". " + nextObject.getDescription());
}
}
As you can see, counter is now a global variable inside whaever class contains the method, and you don't need the iterator at all.
Related
I am not experienced in Java, but I need to create Java snippet node in KNIME.
I am trying to write a short Loop that creates a counter/iterator based on a specific condition. I managed to write the IF THEN ELSE part but I wonder how to put it inside the Loop:
out_user_session_counter = 1;
if (c_session_end.equals("session end")) {
out_user_session_counter = out_user_session_counter + 1;
} else {
out_user_session_counter = out_user_session_counter + 0;
}
The idea is: each time c_session_end.equals("session end")
then out_user_session_counter should be augmented by 1
UPD.: Here is screenshot of data model with loop result (not correct result):
Correct expected result would be 1 1 1 1 2 2 3 3 3 3 3
instead of 1 1 1 1 11 1 11 1 1 1 1
You need the loop counter to persist between rows in the Java Snippet. To do that, you define it in the //Your custom variables: part of the snippet:
int sessionCounter=0;
Then in the //Enter you code here: part:
if("session end".equalsIgnoreCase(c_column2)){
sessionCounter++;
}
out_user_session_counter = sessionCounter;
As shown, the row with 'session end' will contain the incremented counter. If you want it to contain the old counter, change the snippet so that the final line above is before the if statement.
You don't need to put this in a loop in KNIME - the expression part of the snippet is already calculated sequentially for each input row. Shown below in the snippet dialog for clarity
You need to work with for/while loops to achieve that.
int out_user_session_counter = 1; //Step 1
while(c_session_end.equals("session_end")) { //Step 2
out_user_session_counter++; //Step 3
//Step 4 Other code to modify c_session_end.equals("session_end")
}
Step 1. In this step we initialize the variable to 1
Step 2. While the variable is equal to session_end
Step 3. increment the variable by 1 ( note that this is the same as out_user_session_counter = out_user_session_counter + 1; )
Step 4. Code to modify the c_session_end value must be placed so loop exits as some point or the loop will never stop.
What happens in your code is the following => if(c_session_end.equals("session end")) increment the variable by one, otherwise increment it with 0.
If you want it to run multiple times you have to use while or for loops.
You need a simple loop and this will take care of your else part itself.
int out_user_session_counter = 1;
while(c_session_end.equals("session end")){
out_user_session_counter++;
}
Hope this helps.
Well, we don't know what you are looping, but I assume you are looping some Strings.
This example shows how to loop a List<String> containing session events while counting the ones that equal "session end":
public static void main(String[] args) {
// create a data structure for sample data
List<String> sessionEvents = new ArrayList<String>();
// add some sample data
sessionEvents.add("session start");
sessionEvents.add("session somthing");
sessionEvents.add("session end");
sessionEvents.add("session start");
sessionEvents.add("session something");
sessionEvents.add("session something");
sessionEvents.add("session something");
sessionEvents.add("session end");
sessionEvents.add("session start");
sessionEvents.add("session something");
sessionEvents.add("session end");
// define a counter variable (considering Java naming conventions)
int outSessionCounter = 0;
// use an enhanced for loop (aka for-each loop)
for (String sessionEvent : sessionEvents) {
// every time the event is "session end"...
if (sessionEvent.equals("session end")) {
// ... increment the counter
outSessionCounter++;
}
}
// print the result
System.out.println("Amount of session ends is " + outSessionCounter);
}
This just counts occurrences of "session end", but you would have to tell us if you are using a different data structure or maybe even a stream.
Im not exactly sure what you're trying to achieve, but I think I have something
class Result{
public int out_user_session_counter = 1;
public String c_session_end;
}
Result r = new Result();
r.c_session_end = /*[whatever you want it to be];*/ "";
Thread thread = (new Thread(new Runnable() {
int out_user_session_counter = 1;
public void run() {
out_user_session_counter = 1;
while (!Thread.interrupted()) {
if (r.c_session_end.equals("session end")) {
out_user_session_counter++;
r.out_user_session_counter = out_user_session_counter;
}
//code to modify the r.c_session_end
}
}
}));
thread.start();
//to get the result do
int result = r.out_user_session_counter;
this will keep looping indefinitely.(this can go within a method)
you can edit the result object to influence the
Try something like this:
out_user_session_counter = 1;
int loopCycles = 10
for(int i = 0; i < loopCycles; i++){
if (c_session_end.equals("session end")) {
out_user_session_counter = out_user_session_counter + 1;
}
}
You can simply do something like:
out_user_session_counter = 1;
int loop_counter =0;
int rows = 10 //or some other variable that contains the number of rows
while(loop_counter < rows) {
if (c_session_end.equals("session end")) {
out_user_session_counter = out_user_session_counter++;
loop_counter++;
}
System.out.println(out_user_session_counter);
}
All you need to do is add a loop to the outer portion of you code. Any loop will do, I used a while loop. You could use a for loop as well.
for(int i = 0; ii < rows; ii++){
if (c_session_end.equals("session end")) {
out_user_session_counter = out_user_session_counter++;
}
System.out.println(out_user_session_counter);
}
Or, you can use a do-while loop:
int index = 0;
do{
if (c_session_end.equals("session end")) {
out_user_session_counter = out_user_session_counter++;
index++;
}
System.out.println(out_user_session_counter);
} while (index < rows);
It should be noted that with the do-while loop, you will alway enter the loop, even if there are no rows available (the rows are 0).
Also, you don't need the else portion, as there is no point in adding 0 to your value, you can simply skip it.
I am new to java but have been playing with array-lists and am now stuck.
I have an array list created off a class called Car with three parameters one of which is called times moved.
ArrayList:
private ArrayList<Car> list = new ArrayList<Car>() ;
Car class
public Car ( String licenseNum, String carStatus , int timesMoved)
{
licensePlate = licenseNum ;
status = carStatus ;
moved = timesMoved;
}
I am reading input of a file that is supposed to be like a garage and says if a car is "Arriving" or "Departing"
I am trying to write a code using an if statement that says if the status is "Departing" then the current element gets deleted the all elements in front of it add one to their "times moved parameter"
The part I am stuck on is the one where, based on the element getting deleted, all the elements in front of it in the array list add one to their "times moved" parameter.
Would anyone give me some advice on how it would be possible to do that?
I came up with this but it does not seem to work
public void carDepart()
{
for ( int i = 0 ; i < list.size() ; i++ )
{
Car current = list.get( i ) ; // get next car
if (current.getStatus().equals("DEPART"))
{
int pos = list.indexOf(i);
for ( int j = 0 ; pos < j ; j++)
{
current.setTimesMoved(1 + current.getTimesMoved());
}
list.remove(i);
}
break;
}
}
Second method
public void moveCarInGarage()
{
for ( int i = 0 ; i < list.size() ; i++ )
{
Car current = list.get( i ) ; // get next car
if (current.getStatus().equals("ARRIVE"))
{
currentCars++;
if (currentCars <= 10)
{
System.out.println("Car with license plate" +
current.getLicenseNum() + " has been moved into the garage");
}
else
{
System.out.println("The garage is full at this " +
"time so come back later");
}
}
else
{
currentCars--;
System.out.println("Car with license plate" +
current.getLicenseNum() + " is departing and has been moved "
+ current.getTimesMoved() + " times" );
}
}
}
Here is an example of what you can do. In it, I assume you are using getters and setters. You can also call the attributes directly assuming you've set the access modifiers in a way that allows you to do so.
All I did was create a new method called incrementTimesMoved() that iterates through your ArrayList and increments all the "moved" attributes in it's elements until it gets to the one with a given index. It removes this, and stops running.
public class MCVE {
public static void main(String[] args) {
// IMO list isn't very descriptive, so I changed it to carList.
ArrayList<Car> carList = new ArrayList<>();
// Add a bunch of values to carList here.
for(int i = 0; i < carList.size(); i++) {
if(carList.get(i).getStatus().equals("Departing")) {
incrementTimesMoved(i, carList);
return; // stops the method
}
}
}
// only static because I am calling in the main() function
private static void incrementTimesMoved(int index, ArrayList<Car> carList) {
for(int i = 0; i < carList.size(); i++) {
if(i == index) {
carList.remove(index);
return;
}
carList.get(i).setMoved(carList.get(i).getMoved() += 1);
}
}
}
Hi I am new to Java OOP and I have some problems in running the program
The problem is there is no output and the loop never stops.
This is my Main.java
public class Main {
public static void main(String[] args) {
Dice firstDie = new Dice();
Dice secondDie = new Dice();
do {
count++;
if(firstDie==secondDie ){
same=true;
System.out.println("It took "+count+ " times " + firstDie.getValue() + " and " + secondDie.getValue());
}
}
while(!same);
}
}
and this is my Dice.java
public class Dice {
private int value;
public Dice() {
value = (int)(Math.random()*6)+1;
} public int getValue() { return value; }
}
in my Main.java class when I write int count = 0; and boolean same = false; the loop never ends.
Probably you have to compare the value firstDie.getValue() and secondDie.getValue().
What you are trying to do is compare the 2 instances of class Dice . When you compare those, unless they are the same object, it cannot be same.
Hence, same is not made true at all and this goes to a loop.
For more info, please refer, equals() method in java because that is what is called when two instances/objects are compared.
Edit:
Even if you try below code snippet of do-while (ie. compare the value firstDie.getValue() and secondDie.getValue(),
do {
count++;
int first = firstDie.getValue();
int sec = secondDie.getValue();
System.out.println(first+ " "+sec);
if(first==sec ){
same=true;
System.out.println("It took "+count+ " times " + first + " and " + sec);
}
}
while(!same);
It still goes to loop because of the fact that the value of Dice class is set only once i.e. in constructor.
So the ideal way is to write,
public Dice() {
value = 0;
} public int getValue() { return (int)(Math.random()*6)+1; }}
You are comparing the classe's instances and not the values stored within them.
Notice that on your println you are referring to each of the dices values (which is what you want to compare and have match), but on your comparison you are checking the objects.
Since they are 2 different instances they will never match, and therefore there will be no output and the loop never stops.
try firstDie.getValue() == secondDie.getValue() instead of firstDie== secondDie
You are checking for the two instances to be equal if you say firstDie== secondDie. These two are never equal.
I am processing elements of an ArrayList in random order, generally by printing them out. I would like to detect when the randomly selected index is 0 or 1 so as to perform special handling for those cases, where the handling for index 0 is partially dependent on whether index 1 has previously been processed. Specifically, nothing is immediately printed when index 1 is processed, but if it is processed then when index 0 is subsequently processed, both the index 1 and the index 0 values are printed. In any event, the loop is exited after ten iterations or after processing index 0, whichever comes first.
I tried to implement this using if statements, but there where obvious flaws with that one. I have searched everywhere for any examples but found none. I have begun to consider using sorting algorithms or threads to hold the first value found then continue looping until it sees the second the print it out. I would appreciate any help possible.
Here is my code:
public static void random_sortType(){
types = new ArrayList<String>();
types.add("Start");
types.add("Starting");
types.add("Load");
types.add("Loading");
types.add("End");
ran = new Random();
int listSize = types.size();
String tempEventType;//the temp variable intended to hold temporary values
for(int i = 0; i < 10; i++){ //the loop goes round the ArrayList 10 times
int index = ran.nextInt(listSize);//this produces the random selection of the elements within the list
if(index == 0){
out.println(types.get(index));
out.println();
break;
}
if(index == 1){
tempEventType = types.get(index);
if(index == 0){
tempEventType = types.get(0) + " " + types.get(1);
out.println(tempEventType);
break;
}
}/*if(index == 0){
tempEventType = types.get(0) + " " + types.get(1);
out.println(tempEventType);
break;
}*/
//out.print("Index is " + index + ": ");
//out.println(types.get(index));
}
}
You need to store the random generated index into a global variable and update it everytime a random number is generated. It should be something like this.
public static void random_sortType(){
types = new ArrayList<String>();
types.add("Start");
types.add("Starting");
types.add("Load");
types.add("Loading");
types.add("End");
` int previousIndex;
ran = new Random();
int listSize = types.size();
String tempEventType;//the temp variable intended to hold temporary values
for(int i = 0; i < 10; i++){ //the loop goes round the ArrayList 10 times
int index = ran.nextInt(listSize);//this produces the random selection of the elements within the list
previous_index =index;
if(index == 0){
out.println(types.get(index));
out.println();
break;
}
if(index == 1){
tempEventType = types.get(index);
if(previousIndex == 0){
temp EventType = types.get(0) + " " + types.get(1);
out.println(tempEventType);
break;
}
According to your description, these are the basic requirements for your application:
Process ArrayList in random order
Processing = printing value at randomly selected index
Make 10 attempts to process items in the list.
Detect when random index is 1 or 0.
if 1
don't process, but flag it was selected.
if 0 && 1 is flagged
process 0 and 1
exit
If these requirements are correct, here is the implementation I came up with:
import java.util.ArrayList;
import java.util.Random;
import static java.lang.System.*;
public class RandomSort {
private static final int MAX_ATTEMPTS = 10;
private static boolean wasOneSelected = false;
public static void main(String[] args) {
ArrayList<String> types = new ArrayList<>(5);
types.add("Start");
types.add("Starting");
types.add("Load");
types.add("Loading");
types.add("End");
random_sortType(types);
}
public static void random_sortType(ArrayList<String> types) {
Random ran = new Random();
int lastIndex = types.size() - 1; // index range is from 0 to 4
for (int i = 0; i < MAX_ATTEMPTS; i++) {
int index = ran.nextInt(lastIndex);
if ( (index == 0) && wasOneSelected) {
process(types.get(index) + " " + types.get(index + 1));
break;
} else if (index == 1) {
wasOneSelected = true;
} else {
process(types.get(index));
}
}
}
public static void process(String str) {
out.println("Processing: " + str);
}
}
The key here is the inclusion of the boolean wasOneSelected initialized to false. Once it is set to true, it will never be false again for the duration of the application. The if-else block handles all branching within the loop, and I favored wrapping the println call into a method called "process" for readability purposes to align it more closely with your description.
Feedback appreciated :)
I have this code which applies a counter to each item on the list. When the item reaches a certain number it is moved from jList3 to jList 1.
public Map<Object, Integer> buttonMap = new HashMap<Object, Integer>();
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
Integer counter = null;
int[] selection = jList3.getSelectedIndices();
for (int i = 0; i < selection.length; i++){
Object selString = jList3.getModel().getElementAt(selection[i]);
counter = buttonMap.get(selString);
if(counter == null ) {
buttonMap.put(selString, new Integer(1));
}
else {
buttonMap.put(selString, new Integer(counter.intValue() + 1));
}
System.out.println(selString + " has been clicked " + buttonMap.get(selString) + " times.");
try{
if (counter == 4){
listModel2.removeElement(selString);
listModel.addElement(selString);
}
}
catch (NullPointerException npe1) {
npe1.getMessage();
}
}
}
The behavior comes in the if counter == 4 section.
It works fine but here is the weird part that I need help understanding
If I am counting up two items at the same time and they both reach the number that moves them on the same button click.
-It moves 1 of the items
-It does not count up on the other
-Instead it adds +1 to the counter of a non highlighted item
Example:
I am counting on list item 1 and 2, they both reach the max number, 1 gets moved 2 stays put(no increase in count) and item 3 gets +1 to counter
When you remove an element from jList3, the elements that follow are shifted.
I would sort the selection array and scan it in reverse order.
...
int[] selection = jList3.getSelectedIndices();
Arrays.sort(selection);
for (int i = selection.length; --i >= 0; ){
...
UPDATE: the sorting is not required, because the array returned by getSelectedIndices() already is
The problem is that you are modifying one of your model lists in the same loop as in which you are querying it. If you were using an iterator you would actually get a concurrent modification exception, but as it is you are using specific indexing. In either case your logic is now wrong because the indexes you were relying on to query the list have changed. You can sort your selection array in descending order and keep your code the way it is, but I think it will be cleaner and more maintainable to modify your source array after the loop is done, like so:
...
System.out.println(selString + " has been clicked " + buttonMap.get(selString) + " times.");
if (counter == 4) {
listModel.addElement(selString);
}
}
for(Object o : listModel) {
listModel2.removeElement(o);
}