-3

How to get the program to read a set of integers from an input file. The goal is to store them in an array and then display greater values than input. Also, Also, create a method called greater_than_n() that accepts an integer array and an integer n. The purpose of this method is to display numbers greater than n

import java.util.Scanner;
import java.io.*;
public class Lab5 // File Name{
public static void main(String[] args) throws IOException
{
    Scanner userInput = new Scanner(System.in);
    File Integers = new File("Integers.txt"); 
    Scanner inputReader = new Scanner(Integers); 

    String line = inputReader.nextLine(); 
    System.out.print(line); 
    inputReader.close(); 

    System.out.print("Enter an Integer: ");

    int userAction = userInput.nextInt();   
    System.out.println("The numbers in the input file that are greater than " + userAction + " are: ");

    for (int index = 0; index < Integers.length; index++) 
    {
        if(Integers[index] > userAction)
        {
            System.out.print(Integers + " ");
        }
    }
}

}

  • Please learn cycles and arrays... First error - limitation in `for` cycle (`index < userAction`, while should be `index < numbers.length`). Second error - cycle body and output data from array (`System.out.print(numbers);`, while should be `System.out.print(numbers[index]);`) – Pavel Uvarov Feb 15 '17 at 09:40

3 Answers3

1

You are checking in a wrong way. Your loop should iterate up to the length of array. Then if you want to print the greater numbers in the array, check each element if greater than the input number. If yes, print the number in the index.

Scanner userInput = new Scanner(System.in);

int[] numbers = {2, -4, 6, 8, 19};
System.out.print("Enter an Integer: ");

int userAction = userInput.nextInt();
System.out.println("The numbers in the input file that are greater than " + userAction + " are: ");

for (int index = 0; index < numbers.length; index++) {
    if(numbers[index] > userAction)
        System.out.print(numbers[index] + " ");
}
Shahid
  • 2,288
  • 1
  • 14
  • 24
  • How to get the program to read a set of integers from an input file. The goal is to store them in an array and then display greater values than input. Also, Also, create a method called greater_than_n() that accepts an integer array and an integer n. The purpose of this method is to display numbers greater than n – user7549103 Feb 15 '17 at 22:31
1

You are printing out array on every step of loop, to print out a number you should change System.out.print(numbers); for System.out.print(numbers[index]);.

More of than if you want to print just number which greater then input (userAction=3, output should be 6, 8, 19 for your example), you have a error in your algorithm. What your algorithm does is just print out array userAction times. To fix it you can use this snippet:

for (int index = 0; index < numbers.length; index++)
{
    if (numbers[index] > userAction) {
         System.out.print(numbers[index]); 
    }
}
Vlad Bochenin
  • 3,007
  • 1
  • 20
  • 33
-1

You print the Arrayobject, what you want is to print the index of the array, this can be accomplished by using:

    System.out.print(numbers[index]).
halra
  • 11
  • 3