How to put values from ArrayList in certain ranges? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is it possible to put values from my ArrayList into a specific ranges?
My ArrayList contains doubles:
0.4987873968412172
0.49949149542775684
0.4995227496071881
0.4999386804321369
0.500520813392628
0.5005912302221284
0.5006590304457654
0.5009476185730563
0.5013192031470984
And I want to divide them into (for example) two ranges to get an output:
Numbers from 0.498 to 0.499
0.4987873968412172
0.49949149542775684
0.4995227496071881
0.4999386804321369
Numbers from 0.500 to 0.501
0.500520813392628
0.5005912302221284
0.5006590304457654
0.5009476185730563
0.5013192031470984

List<Integer> range1 = new ArrayList<>();
List<Integer> range2 = new ArrayList<>();
for(Integer i: myArrayList)
if(i < 0.5) range1.add(i);
else range2.add(i);

Yes, use if and else
if (number > 0.498 && number < 0.499) {
firstArrayList.add(number);
}
else
{
secondArrayList.add(number);
}
And if you have 3 (or more) ranges use else if
if (number > 0.498 && number < 0.499) {
firstArrayList.add(number);
}
else if (number > 0.500 && number < 0.501)
{
secondArrayList.add(number);
}
else
{
thirdArrayList.add(number);
}

Related

java program to loop and output 2,5,7,10,12 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
Loop
how to loop this without using array? I'm clearly confuse in this in java module I don't know the solution ;-; I'm new to java pls helppp mee
Try this.
for (int i = 5; i <= 25; i += 5)
System.out.println(i / 2);
Or
for (int i = 1; i <= 5; i++)
System.out.println(i * 5 / 2);
output:
2
5
7
10
12
Think for yourself why this produces the correct result.
public class Main {
public static void main(String[] args) {
int outputNum = 0;
for(int i = 0; i < 5; i++){
if (i % 2 == 0){
outputNum += 2;
}
else {
outputNum += 3;
}
System.out.print(outputNum + (i == 4 ? "" : " "));
}
}
}
This does exactly what you need.
Explanation:
In the loop if the i variable is even we add 2 to the output number (since you need to add 2 then 3 then 2...) and if i is uneven then we add three. At the end of the for loop we print the number without the newline using System.out.print and we separate them by a space if we aren't printing the last number using a ternary operator (i == 4 ? "" : " ") which returns a space if i is not equal to 4 and an empty string if it is, we do this to avoid having a space on the end of our output, so we get this 2 5 7 10 12 instead of this 2 5 7 10 12

while (1) loop with continue and break statements [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need help with a while(1) loop that contains a continue and break statement. It must count from numbers 1 to 20 and for every even number, it must output the values. I have to use a continue after my writeToPage statement and use a break statement when it reaches 20.
This is what I tested out but the file will not even load:
writeToPage("Program 4: Continue and Break");
writeToPage("");
while(1) {
if (i % 2 == 0){
writeToPage(+ i);
continue;
}
if (i >= 20){
break;
}
}
I'm not sure if I'm putting them in the wrong place.
For infinite loop, you need to write while(true) instead of while(1).
If i % 2 != 0 is true, simply increment i by 1 and continue; otherwise, print the value of i and increment i by 1.
Demo:
public class Main {
public static void main(String[] args) {
int i = 1;
while (true) {
if (i % 2 != 0) {
i++;
continue;
} else {
writeToPage(i);
i++;
}
if (i >= 20) {
break;
}
}
}
static void writeToPage(int i) {
System.out.println(i);
}
}
Output:
2
4
6
8
10
12
14
16
18
20

how to stop the code when the code has executed a certain amount of times? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Name: runSequence4
Input: Intake feed, int n
Output: none
Action: Takes in an intake object and calls the method give(). It first passes the number 1 into
give and then alternates between -1 and 1. It does this n number of times.
How do I stop the loop when the code executes it "N" times?
This is what i have so far
public void runSequence4(Intake feed, int n){
for(int x = 1; ???? ; x = (x * -1)){
feed.give(x);
}
}
Just add a variable i to store the current iteration and test whether it has exceeded n
public void runSequence4(Intake feed, int n){
for(int x = 1, i = 0; i < n; x = (x * -1), i++){
feed.give(x);
}
}

Finding the sum of the two greatest numbers of 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
With Java, how would one find the greatest two numbers of a set of 3 numbers without using if conditionals.
For example given the 3 numbers {2,3,5}
int a = 2;
int b = 3;
int c = 5;
int total;
total would be replaced with the value of c+b = 8
List<Integer> data = Arrays.asList(23,6,13);
Collections.sort(data);
Collections.reverse(data)
data = data.sublist(0,2);
System.out.println(data);
One line:
int biggest = (a > b ? a : b) > c ? (a > b ? a : b) : c;
Two lines:
int firstStep = (a > b ? a : b);
int biggest = firstStep > c ? firstStep : c;
Java 8:
int max = Arrays.stream(numbers).max().getAsInt();
int sec_max = Arrays.stream(numbers).filter(i -> i != max).max().getAsInt();

Write a program that returns number of decimal digits in the given input string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Write a program that returns number of decimal digits in the given input string.
ex :
Input: 2345abc423
output: 7
This is my code:
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int len = s.length();
int numbers = 0 ;
for(int i=0; i < len; i++){
char c = s.charAt(i);
int num = (int) c;
while(num <= 57 && num >= 48){
System.out.println(numbers);
numbers++;
}
}
System.out.print(numbers);
/////
sorry because my question wasn't obvious
my program always return 0 , not the number of decimal digits
thanks #Keppil i tried if and it worked
thanks
Your program will never exit the while loop since the condition never changes. You should just use an if instead:
if (num <= 57 && num >= 48) {
System.out.println(numbers);
numbers++;
}
Solution that abuses regex:
System.out.println(input.replaceAll("\\D", "").length());

Categories