1
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.

5
  • Have you read javadocs or any tutorials to deal with Lists ? Commented Mar 6, 2014 at 3:39
  • 1
    Are you trying to get the particular value or any other? Commented Mar 6, 2014 at 3:40
  • @Sanjeev I've searched up what I can think of that would relate to this but I didn't have any luck. Is there any chance you can lead me in the right direction? Commented Mar 6, 2014 at 3:41
  • @newuser I'm trying to print the number of values that are greater or equal than what the user enters. For example, if the ArrayList contained {1,2,3,4,5} and the user entered 4, the code should print 2. Commented Mar 6, 2014 at 3:42
  • If this operation (user entering a integer) is one time thing, you can implement the usual way of scanning the entire list. But if user can do multiple operations on this data, then you should probably sort the data (or use a binary search tree). Commented Mar 6, 2014 at 3:47

3 Answers 3

1

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++;
   }
 }
Sign up to request clarification or add additional context in comments.

Comments

0
int counter=0;
for(Integer current: myArrayList){
  if(current>=targetNumber){
     System.out.print(current+" ");
     counter++;
  }
}

Comments

0

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++;
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.