3

I want to count a range of number with a for loop using input from scanner.

Tried writing the code but still no results

package com.example.myapplication;

import java.util.Scanner;

public class dsd {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter first number:");

        int firstnum = sc.nextInt();
        System.out.println("Enter first number:");

        int secondnum = sc.nextInt();
        System.out.print("counting ");


        for (int i = firstnum; i >= secondnum; i++) ;
        {
            System.out.print(i);
        }

    }

Upper limit :5

Lower Limit :2

Expected to see COunting 2 ,3 ,4, 5

Nghia Bui
  • 3,694
  • 14
  • 21
elmo
  • 31
  • 2
  • 1
    Typo, change `>=` to `<=` (assuming `firstnum` holds lower limit and `secondnum` upper limit). – Pshemo May 19 '19 at 15:10
  • But i keep getting the error of unable to find variable i? – elmo May 19 '19 at 15:11
  • 2
    Another typo, you have `;` at the end of `for` loop line. More info [Semicolon at end of 'if' statement](https://stackoverflow.com/q/14112515) – Pshemo May 19 '19 at 15:11

4 Answers4

1

I think it would be better to use a camel case to match the coding convention. And if you put a semicolon after the for loop, the following code will not be executed. Of course, the conditional statements in the for loop seem a little wrong.

For the example given in the question, see the code below:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Upper limit:");
    int firstNum = sc.nextInt();

    System.out.print("Lower Limit:");
    int secondNum = sc.nextInt();

    System.out.print("counting ");
    for (int i = secondNum; i <= firstNum; i++) {
        System.out.print(i);
    }
}

Result (in the terminal)

Upper limit:5
Lower Limit:2
counting 2345
Madplay
  • 1,027
  • 1
  • 13
  • 25
0

I have seen this mistake many times:

for (int i = firstnum; i >= secondnum; i++) ;

Please remove the ;

Nghia Bui
  • 3,694
  • 14
  • 21
0

Is the first number always greater than the second?

If so, you should write it as the code below.

// Delete semicolon. and less then change 
for (int i = firstnum; i <= secondnum; i++) {
    System.out.print(i);
}
SangHoon Lee
  • 243
  • 2
  • 5
0

In your code you have used the "sc" scanner for both firstnum/secondnum variable. The loop of your code will print only the last number inserted.


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

         int i;
         int start, end;

         Scanner sc = new Scanner(System.in);
         Scanner sc1 = new Scanner(System.in);

         /*
          * The block try-catch highlights the fact that you cannot insert min
          * number bigger than the max number
          */
         try {

             System.out.print("Enter first number:");
             start = sc.nextInt();

             System.out.print("\nEnter second number: ");
             end = sc1.nextInt();
             if(start > end) throw new Exception();
             i = start;


             while (i<=end){
                 System.out.println("count: " + i);
                 i++;
             }

         }catch(Exception e){
             System.out.println("You cannot start with number bigger than 'end-number' ");
         }


    }
}
edhgoose
  • 887
  • 8
  • 26