0
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter two numbers: ");
    int a = sc.nextInt();
    int b = sc.nextInt();
    System.out.println("Enter two decimal numbers: ");
    float x = sc.nextFloat();
    float y = sc.nextFloat();
    System.out.println("Enter two words: ");
    String i = sc.nextLine();
    String j = sc.nextLine();
    //System.out.println(i);
    //System.out.println(j);
    System.out.println(max(a,b));
    System.out.println(max(x,y));
    System.out.println(max(i,j));
}

I'm still learning java, I tried removing "Line" and writing my two words inputs in the same line separated by a space and it worked but can someone explain why "nextLine" skips the first String input even though it executes both ints and both floats ?

1 Answers1

-1

This seems to work for me. When printing your values you had commas, in Java you need to put the addition symbol in order to add two values together, Also I had to add a string in between in order to make sure the program did not add the numerical values, I only added it within the last print line in order to make the output look nicer. And I changed .nextline() to a .next() which can only input single words.

 import java.util.Scanner;
    public class Test {
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter two numbers: ");
                int a = sc.nextInt();
                int b = sc.nextInt();
                System.out.println("Enter two decimal numbers: ");
                float x = sc.nextFloat();
                float y = sc.nextFloat();
                System.out.println("Enter two words: ");
                String i = sc.next();
                String j = sc.next();
                //System.out.println(i);
                //System.out.println(j);
                System.out.println(a + " : " + b);
                System.out.println(x + " : " + y);
                System.out.println(i + " : " + j);
            }}
Eny1_1
  • 1
  • 1
  • Please try not to provide answers to questions who have already been answered before. If you have to add anything new, you can add that to the original question. Also, try to provide explanation when answering like why does your approach work. – AKSingh May 19 '21 at 15:31
  • I will try next time sorry. – Eny1_1 May 19 '21 at 15:45