1

I am pretty new to JAVA. I want to know how I can create a little program where the user can have the start and the end input (2 user inputs). And then count the in between numbers. Like this for example:

  • User input 1 = 5; User input 2 = 10; The result should be: 5,6,7,8,9,10.

After some research I got this code but something is wrong, I really appreciate the help!

 import javax.swing.*;
 class Users{
    public static void main(String[] args) 
    {
        int a = userinput();
        int b = userinput();
        for( a = a <= b ; a = a + 1)
        {
            System.out.println(a);
        }
    }

public static int userinput()
{
    String tekst = JOptionPane.showInputDialog(null, "enter a number", "Users",3);
    int number = Integer.parseInt(tekst);
    return number;
}
}
2
  • 3
    Because you have already declared and basically initialized the variabel a, you can do this: for(; a <= b; a++) { System.out.println(a); }. Commented Jun 13, 2021 at 11:41
  • Please provide the details what exactly is wrong. I guess, there is a compilation error because the syntax of for loop is incorrect. It could be fixed as for (; a <=b; a++) { System.out.println(a); } Commented Jun 13, 2021 at 11:42

4 Answers 4

4

You had a little error in your for loop.

Plus I'd use a special variable only for counting/iterating, in order not to mess up the input:

import javax.swing.JOptionPane;

class Users {
    public static void main(final String[] args) {
        final int a = userinput();
        final int b = userinput();
        for (int counter = a; counter <= b; counter++) {
            System.out.println(counter);
        }
    }

    public static int userinput() {
        final String tekst = JOptionPane.showInputDialog(null, "enter a number", "Users", 3);
        final int number = Integer.parseInt(tekst);
        return number;
    }
}

Also, if you wanna increase a value, you can to

a++;
++a;
a+=1;
a=a+1;

which are more or less the same.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thank you for your explanation. I understand it better now, really appreciated it.
1

(This is not gui based solution, it displays in console)

First I think you should learn scanner in java. you need to import scanner

import java.util.Scanner;

Then you need to create a scanner object inside your main method.

Scanner keyboard = new Scanner(System.in);

You can take input with this code:

System.out.println("Enter first number");
int firstInput=keyboard.nextInt();
System.out.println("Enter second number");
int secondInput=keyboard.nextInt();

After you have inputs you can make a for loop to count between

for(int i=firstInput;i<=secondInput;i++){
    System.out.print(i);
} 

I hope this helps your problem.

Comments

0

Use scanner instead of joption if you are new. import this in top of the line.

import java.util.*; 

Write below code in your function to take input

 Scanner sc= new Scanner(System.in);    //System.in is a standard input stream  
 System.out.print("Enter number- ");  
 int number= sc.nextInt();  

1 Comment

This uses the Console, and not Swing. It's a bit besides the actual problem, because that's not what solves his question. And to counter your "Use scanner instead of joption if you are new.": in my experience its best to get people away from console input as soon as possible, and towards using proper UI. You can't imagine to what lengths they go to display stuff and make stuff work in console I/O that they could easily have done with Swing/JavaFX. So as he's already using Swing, he best stick with it. IMHO
0

import scanner class at top.

import java.util.*; //here is a code to create new user defined variable.

Scanner sc= new Scanner(System.in);

// Create a Scanner object
System.out.println("Enter first variable");

int firstVariable= sc.nextInt();

System.out.println(" First Variable is: " + firstVariable); // Output user input

int secondVariable= sc.nextInt();

System.out.println(" Second Variable is: " + secondVariable); // Output user input

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.