Question

I am taking an online MOOC to learn Java, the only problem i have is it is from the university of Helsinki in Finland i live in the US so there are limited times when i can be awake to ask for help on an exercise. my current exercise is to ask the user for a number and then print each whole number up to that number while using a

    while {
   } 

statement

this is the code i have currently

    public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    int number = Integer.parseInt(reader.nextLine());
    System.out.print("up to what number?:"+ number);
    while (number<=number){ 
        System.out.println(number);
        number++;
    }
}

and what it looks like it is doing is ignoring the

    while (number<=number) {
    System.out.println(number);

part of my code and proceeding straight to the

    number++;

portion of my code do i need to declare another int(variable) to store a value? the way the course has the test cases for grading i can't simply declare a variable with a definite value as they run several test cases like positive numbers and negative numbers. is there a way to use the reader to store a value to two separate variables so that they can be compared against each other and only print the numbers up to that number?

i also know that i am missing a

   Break;

statement but i am not sure where i would place it in the code i have, i have tried to use

    } else {
     break;

but get an error stating that i have an else without an if. i am using netbeans as it is required for my course because the server submissions are setup through TMC.

thinking about it now i'm sure its not skipping the while statement but simply continues to print because as it prints and increments the users input is incremented as well since i only have the one variable but i am again not sure how i would go about storing the user input value in two different variables where i can compare them with the less than or equal to statement and stop the printing once it reaches the number input by the user, in that case i would not necessarily need the break statement as it would stop once it prints up to the number input.

ANSWERED: here is what i finally came up with as my answer.

    public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    System.out.print("up to what number?:");
    int numbers = 1;
    int number = Integer.parseInt(reader.nextLine());

    while (numbers <= number){ 
        System.out.println(numbers);
        numbers++;
    }
}
Was it helpful?

Solution 2

You need another variable to increment upto inserted number

int i=1;
while (i<=number){ 
        System.out.println(i++);
    }

What your loop is doing

 while (number<=number){ 
        System.out.println(number);
        number++;
    }

for example number=10 so it's checking like this 10<=10 do you need this,absolutely not.

So for your code you need an another variable to increment up to entered number.

OTHER TIPS

You are comparing number to itself. So (number <= number) is always true.

Use a different variable, such as count to actually count up. Initialize it at zero.

Change the condition to (count < number), then in the loop change the increment to count++ and then output count.

Oh, and you should probably prompt for the number before you read it in.

ie your whole program will be:

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    System.out.print("up to what number?:");
    int number = Integer.parseInt(reader.nextLine());
    int count = 0;

    System.out.println(number);

    while (count<number){ 
        count++;
        System.out.println(count);
    }
}

This would do it:

public static void main(String[] args) {
 int startingInt = 1; //begin printing from 1
 System.out.println("Up to what number?");
 Scanner reader = new Scanner(System.in);

 int number = Integer.parseInt(reader.nextLine());

 while (startingInt <=number){ 
    System.out.println(startingInt);
    startingInt++;
 }
}

i'm c# expert, so first of all please use c#. But I know I know you can't always select your laguagued, ;)

Here is solution , it works on my machine.

while (number<=number){ 
    System.out.println(number);
    number++;
    if (number==arg[0]) break;
}

Enjoy the solution!

System.out.println("Up to what number?");
    int number = Integer.parseInt(reader.nextLine());
    int n = 1;
    while (n <= number) {
        System.out.println(n);
        n++;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top