0

I want to take a string input from keyboard. But when I ran this program there are no option to take string input. I don't know what my fault is.

Here my code is:

import java.util.Scanner;
public class Input_Program {
public static void main(String args[])
{
    Scanner in = new Scanner(System.in);
    int a,b;
    System.out.println("Enter the first number :");
    a=in.nextInt();
    System.out.println("Enter the second number :");
    b=in.nextInt();


    System.out.println("Value of first number:"+a);
    System.out.println("Value of second number:"+b);

    System.out.println("Do you want Add two numbers");
    System.out.println("To Continue: Type Yes");

    String S=in.nextLine();

    if("Yes".equals(S)||"yes".equals(S))
    {   
    int sum=a+b;
    System.out.println("Summation :"+sum);  
    }

}  
}

I want to take an input from this code. But it's not working.

    String S=in.nextLine();

    if("Yes".equals(S)||"yes".equals(S))
    {   
    int sum=a+b;
    System.out.println("Summation :"+sum);  
    }  

And the result of this code is :

run:
Enter the first number :
2
Enter the second number :
3
Value of second number:3
Do you want the Summation of two numbers
To Continue: Type Yes

BUILD SUCCESSFUL (total time: 9 seconds)

  • 2
    try next() instead of nextLine(). According to the scanner documentation, nextLine() "Advances this scanner past the current line and returns the input that was skipped." – Meepo Dec 11 '17 at 06:05
  • It's also return the same result. – Amin Kaiser Dec 11 '17 at 06:09
  • I ran it and it worked for me – Meepo Dec 11 '17 at 06:09
  • This is a duplicate to this [SO Post](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo). You should read it as it will help you. – DevilsHnd Dec 11 '17 at 06:37
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – DevilsHnd Dec 11 '17 at 06:39

1 Answers1

2

1) nextLine() according to https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html "Advances this scanner past the current line and returns the input that was skipped" which is not the behavior you are looking for.

2) It seems that you are new to java, and there are naming conventions. Please do not start variable names with uppercase letters

3) I fixed your indentation, and also generalized the input to take in anything that starts with y. In the future make sure you write your code this way since it's easier to tell which lines of code are in if statements, and loops and such.

import java.util.Scanner;
public class Input_Program {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the first number :");
        int a = in.nextInt();
        System.out.println("Enter the second number :");
        int b = in.nextInt();


        System.out.println("Value of first number:" + a);
        System.out.println("Value of second number:" + b);

        System.out.println("Do you want Add two numbers");
        System.out.println("To Continue: Type Yes");

        String s = in.next();

        if(s.toLowerCase().contains("y")){   
            int sum=a+b;
            System.out.println("Summation :"+sum);  
        }

    }  
}
Meepo
  • 358
  • 3
  • 18
  • #2 naming conventions are same for all the programming languages. – Developer Dec 11 '17 at 06:18
  • Will you please explain this line if(s.toLowerCase().contains("y")) – Amin Kaiser Dec 11 '17 at 06:25
  • of course! s.toLowerCase() takes the string s and converts it to all lowercase letters. then, calling .contains("y") means that if the string has the letter y in it, then it returns true. However, if you want to be more specific, try .startsWith('y'), which will return true if the string starts with the letter y. – Meepo Dec 11 '17 at 06:27
  • if you want to find out more about strings, look at this page: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html in general if you want to know how to use something, search it up and then add the word documentation to your search. The oracle page will tell you what it is, and what methods can be called. – Meepo Dec 11 '17 at 06:29
  • I got it. Thanks <3 – Amin Kaiser Dec 11 '17 at 06:30