So I’m learn java for the first time and can’t seem to figure how to set up a while loop properly . my assignment is Write a program that reads integers, finds the largest of them, and counts its occurrences. But I have 2 problems and some handicaps. I’m not allowed to use an array or list because we haven’t
Tag: loops
How can I do the input loop that exits the program?
“Enter a word or press ‘Q’ to quit” but I don’t know how to do it. It seems confusing to me a little bit. This is my first time coding in Java and I’m still learning. Answer
(Java) How can I make a loop that will assign random names I’ve generated from an array to multiple strings?
Relatively new to Java, looking for a solution to a crucial part of a game I’m making for a class. The idea is to make a very simple stock market simulation game, but the problem is related to creating made-up company names. I have three arrays for the first, middle, and last names of companies. I’m trying to make some
Asynchronous execution/operation with CompletableFuture in Java 8+
In Java, is calling get() method while looping on CompletableFuture instances as good as doing synchronous operations although CompletableFuture is used for async calls? Answer ‘get()’ waits until the future is completed. If that’s what you want, it’s what you use. There’s no general rule. For example, you might be using a method that is inherently asynchronous, but in your
Program that stores the names and populations of 12 countries two arrays of the same size
Use data below Countries: USA, Canada, France, Belgium, Argentina, Luxembourg, Spain, Russia, Brazil, South Africa, Algeria, Ghana Population in millions: 327, 37, 67, 11, 44, 0.6, 46, 144, 209, 56, 41, 28 Use two arrays that may be used in parallel to store the names of the countries and their populations. Write a loop that neatly prints each country name
No ConcurrentModificationException during index based iteration
I have following code: I expected it to throw ConcurrentModificationException but it is working fine. Can some explain the reason? Answer The reason is you are not technically iterating the List. Instead you are random accessing the list using a incrementing index, and removing some values. If you change to code like this to iterate the list it will throw
Java Contain method cannot find symbol
I am trying to only print strings containing the word “The” from a text file but the contain method does not seem to be working. Is there any other way to do this or fix the contain method? } Answer You’re calling contains on Scanner object, you should call it on the lines that you read from file, that’s the
Is there a way to go back by one iteration in a for-each loop?
For example, if I were to do… If it isn’t possible, is there another method of traversing through some sort of collection class while controlling the iteration counter? I tried looking through the answers to this question but couldn’t think of a way that was clear to me. Any suggestions are appreciated! Answer It depends on what you are trying
Return a value present from recursive function in Java
I am trying to extract a value from a while loop. In the output, I am able to capture the value in the Else statement but not able to return it when it is in the console log from the main statement. I want to be able to return “103” when I call the getValueFromkey() function. Sample XML Output: From
creating multiple threads with a unique id (threads do not overlap)
UPDATE I’m solving a producer / consumer problem, and I want to create a few producers and consumers (several threads) and I have a question how can I create several threads correctly so that one task is not performed by two threads (each thread does a different task). code: I tried to do it just in a loop like here: