2

I'm trying to make a "for" loop in which it asks the user to input 10 numbers and then only print the positives.

Having trouble controlling the amount of inputs. I keep getting infinite inputs until I add a negative number.

import java.util.Scanner;

public class ej1 {
    public static void main(String args[]) {

        int x;

        for (x = 1; x >= 0; ) {
            Scanner input = new Scanner(System.in);
            System.out.print("Type a number: ");
            x = input.nextInt();
        }
    }
}
Makoto
  • 96,408
  • 24
  • 164
  • 210
PBRD
  • 67
  • 3
  • 9
  • [The for Statement](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html), [The if-then and if-then-else Statements](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html), [Arrays](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) as some ideas – MadProgrammer Apr 16 '15 at 01:05
  • For assistance, you will need to provide effort and a direct question stating the issue you are having – MadProgrammer Apr 16 '15 at 01:07
  • I'm having trouble to control the amount of inputs (scanner) the loop is going to ask the user. I just keep getting infinite inputs until I add a negative number. – PBRD Apr 16 '15 at 01:10

2 Answers2

5

From a syntax point of view, you've got several problems with this code.

  • The statement for (x = 1; x >= 0; ) will always loop, since x will always be larger than 0, specifically because you're not introducing any kind of condition in which you decrement x.

  • You're redeclaring the scanner over and over again. You should only declare it once, outside of the loop. You can reuse it as many times as you need.

  • You're going to want to use nextLine() after nextInt() to avoid some weird issues with the scanner.

    Alternatively, you could use nextLine() and parse the line with Integer.parseInt.

That said, there are several ways to control this. Using a for loop is one approach, but things get finicky if you want to be sure that you only ever print out ten positive numbers, regardless of how many negative numbers are entered. With that, I propose using a while loop instead:

int i = 0;
Scanner scanner = new Scanner(System.in);
while(i < 10) {
    System.out.print("Enter a value: ");
    int value = scanner.nextInt();
    scanner.nextLine();
    if (value > 0) {
        System.out.println("\nPositive value: " + value);
        i++;
    }
}
   

If you need to only enter in ten values, then move the increment statement outside of the if statement.

i++;
if (value > 0) {
    System.out.println("\nPositive value: " + value);
}

As a hint: if you wanted to store the positive values for later reference, then you would have to use some sort of data structure to hold them in - like an array.

int[] positiveValues = new int[10];

You'd only ever add values to this particular array if the value read in was positive, and you could print them at the end all at once:

// at the top, import java.util.Arrays
System.out.println(Arrays.toString(positiveValues));

...or with a loop:

for(int i = 0; i < positiveValues.length; i++) {
    System.out.println(positiveValues[i]);
}
Community
  • 1
  • 1
Makoto
  • 96,408
  • 24
  • 164
  • 210
  • 1
    was just about to post an answer saying pretty much the same thing, but you beat me to it. Good answer, and very informative. Good job mate. – Ungeheuer Apr 16 '15 at 01:30
  • Thanks a lot. The code works, but I'm still trying to figure out how this works. – PBRD Apr 16 '15 at 01:32
  • @user2698021 What are you stuck on, I can give you a hand. Im trying to avoid homework, so if you need help, let me know. – Ungeheuer Apr 16 '15 at 01:32
  • Well, for instance what does "scanner.nextLine();"? – PBRD Apr 16 '15 at 01:36
  • Scanner.nextLine() pulls the next line of input and makes it a String – Ungeheuer Apr 16 '15 at 01:37
  • Ohh... I get it. If I wanted the positive numbers printed at the end, how would that go? – PBRD Apr 16 '15 at 01:42
  • If you needed them printed at the end, then you'd require an array or a `List` structure of some kind, to which you'd have to print out all at once. I'll add an addendum. – Makoto Apr 16 '15 at 01:42
  • @user2698021 Makoto can help you, i suggest you google the Scanner API for Java, it is fantastic once you get past the technical language – Ungeheuer Apr 16 '15 at 01:43
0
Scanner scan = new Scanner(System.in);
int input=-1;
for(int i=0;i<10;i++)
{
 input = sc.nextInt();
if(input>0)
System.out.println(input);
}
Sashi Kant
  • 12,422
  • 9
  • 38
  • 65
  • I get a "variable i is already defined in method" and it points at i=o under the declaration inside for. – PBRD Apr 16 '15 at 01:19
  • 2
    *"Try this"* - Why? You have a new developer who is having difficulty grasping the concept, some hand holding and explanation of why your solution might suit their issue (as apposed to some other possible solution) would allow them to make better decisions and help them learn - IMHO – MadProgrammer Apr 16 '15 at 01:20
  • @user2698021: Kindly check now – Sashi Kant Apr 16 '15 at 01:27
  • 1
    @MadProgrammer is totally correct, not only can you not simply drop code on a new person, but you cannot drop bad code on a new person. If you want to write an answer, make sure you explain its function extensively. Explain the difference between the OP's code and yours and show why yours functions correctly or better. – Ungeheuer Apr 16 '15 at 01:27