Example: When the user inputs 3, then in the next line, he must input only 3 numbers, too. If he exceeds or lacks, a message must prompt him.
here's my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class example {
public static void main(String[] args) throws FileNotFoundException {
File textFile = new File("newfile.txt");
try (Scanner scanner = new Scanner(textFile)) {
while (scanner.hasNext()) {
int firstNumber = scanner.nextInt();
int array[] = new int[firstNumber];
int compute = 1;
for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextInt();
if (array[i] > 0) {
compute *= array[i];
}
} System.out.println("The maximum product is " + compute + ".");
}
}
}
}
BTW, i'm using netbeans, and we are required to use textFile, that is to get user input in textFile. Please help. Thank you very much.

New Topic/Question
Reply



MultiQuote






|