3

I am new to java coding. I have written the code below to the problem: How can I write a statement that can be used in a Java program to read two integers and display the number of integers that lie between them, including the integers themselves?

I couldn't run it in Eclipse. When I try to run it through Eclipse, it tells me "The selection cannot be launched, and there are no recent launches. Anyway, can someone please tell me if this code correct? Are there any errors in it?

import java.util.Scanner;
public class Assignment4 {
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Enter the first integer:10");
    int first = s.nextInt();
    System.out.print("Enter the second integer:20");
    int second = s.nextInt();
    System.out.println("How many integers are between "+first+" and "+second+"???");
    }
}
Jeetendra
  • 133
  • 2
  • 15
user597553
  • 31
  • 1
  • 2

5 Answers5

2

Please put some more effort on your query, for sure you can come up with your answers by yourself and you will learn faster. For now You can refer to below answer.

package com.barnwal.jeetendra.learn;
import java.util.Scanner;

public class Assignment4 {

    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Enter the first integer:");
    int first = s.nextInt();
    System.out.print("Enter the second integer:");
    int second = s.nextInt();
    System.out.println("How many integers are between " + first + " and "
            + second + "???");

    // To print number of integer between entered number
    if (second > first) {
        System.out.println("Answer : " + (second - first - 1));

        // To print the numbers
        for (int i = first + 1; i < second; i++)
            System.out.print(i + " ");
    } else {
        // To print number of integer between entered number
        System.out.println("Answer : " + (first - second - 1));

        // To print the numbers
        for (int i = second + 1; i < first; i++)
            System.out.print(i + " ");
    }
 }
}
Jeetendra
  • 133
  • 2
  • 15
2

To avoid the unnecessary if-else statement to look which value is bigger, you can also use the functionality of the class java.lang.Math like this

Scanner s = new Scanner(System.in);
System.out.print("Enter the first integer:");
int first = s.nextInt();

System.out.print("Enter the second integer:");
int second = s.nextInt();

int small = Math.min(first, second) ;
int big = Math.max(first, second);

System.out.println("How many integers are between " + small +  " and " + big + "???");
System.out.println("Answer : " + (big - small + 1));

// To print the numbers
for (int i = small; i <= big; i++)
    System.out.print(i + " ");
meiwei92
  • 46
  • 4
1

You can use looping like this:

if (first > second){
     big = first;
     small = second;
}
else if (second > first){
     big = second;
     small = first;
}
for (int i = small; i <= big; i++)
     System.out.print(i + " ");
1

first of all, when you use resources (System.in) you should close them. You can do it with try-finally or you can use try-with-resources. Here is your code:

import java.util.Scanner;
public class Assignment4 {
    public static void main(String[] args) {
        try (Scanner s = new Scanner(System.in)){
            System.out.print("Enter the first integer:10");
            int first = s.nextInt();
            System.out.print("Enter the second integer:20");
            int second = s.nextInt();
            System.out.println("How many integers are between "+first+" and "+second+"???");

            if (first != second)
                System.out.println("Answer: " + Math.abs(first-second - 1));
            else
                System.out.println("Answer: 0");
        }
    }
}
jker
  • 465
  • 3
  • 13
0

How about this:

int diff = second - first - 1;

let secont = 25 and first = 23 so the output will be:
25-23-1 = 1;
which is 24
ErShakirAnsari
  • 653
  • 7
  • 19
  • 1
    You overlooked the "including the integers themselves". The answer in this example should be 3. – Henry Oct 13 '18 at 04:51