Reputation: 57
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class Test
{
public static void main (String args[]) throws java.io.IOException
{
Scanner s = new Scanner(new File("D:\\Work\\Semester 2\\Prog\\population.txt"));
ArrayList<Integer> list = new ArrayList<Integer>();
while (s.hasNext()){
if(s.hasNextInt()){
list.add(s.nextInt());
} else {
s.next();
}
}
s.close();
System.out.println("Enter a number.\n");
Scanner scan = new Scanner(System.in);
String input = scan.next();
scan.close();
int inputNum = Integer.parseInt(input);
int number = 0;
if(number==0){
String line = list;
Scanner scanner = new Scanner(list);
int lineNum = Integer.parseInt(line);
}
}
}
So far I have managed to retrieve integers from a file and insert them into an ArrayList. Now I need to take an input from the user and it needs to display the number of of values (from the ArrayList) that is greater than or equal to the number the user entered. As you can see I'm trying to think of how to scan the ArrayList but I'm currently lost as to what I need to do, any help would be much appreciated.
Upvotes: 1
Views: 1104
Reputation: 8466
Try this,
Collections.sort(list); // because the list may contains unordered values
int counter = 0;
for(Integer value : list)
{
if(value >= enteredValue)
{
System.out.println(value);
counter++;
}
}
Upvotes: 0
Reputation: 2189
int counter=0;
for(Integer current: myArrayList){
if(current>=targetNumber){
System.out.print(current+" ");
counter++;
}
}
Upvotes: 0
Reputation: 201437
Add a counter, iterate over the list, and increment the counter if your condition is met - something like this (using the for each notation),
int count = 0;
for (Integer i : list) {
if (i >= testValue) {
count++;
}
}
Upvotes: 1