Wednesday, April 7, 2021

thumbnail

How to take string user input using scanner

 In our Daily code life, String is playing a vital role. In this blog, we will discuss taking string value input from users. The user can give any string input. Our goal should be code should work and it should give the desired output. 

  Scanner class having one method next line(). This method job is taking input string value from the console. Before seeing.

 In the previous blog, I posted how to take int value from users. You can check it out. 

next() method and nextLine() method is using most of time. we will see first what is next() method and how to use it by one code bit example. One more thing when to use next() and next Line(). we will discuss.

"When you feeling like stopping think about why you started. "

next() method is used for taking input till space. it can't read input separated by space. The next line() method is used for reading input separated by space. this is a main difference between next() and  next Line() .

if you want to read only one word you can use the next() method. 

more than one word like one sentence or line better to go next Line() method.

Sometimes you don't know what user gives input so my recommendation is to always use the next Line() for the safer side. While interview time, you always write your code by taking user input. it will help to save your time changing input. the interview would be impressed about the way of your writing code. 

Let's see the below code you will get to know how to use it in code. 

Steps  : 

// Create Object of Scanner 

// Inform the user for input 

// hold the input in one string variable 

// display the result 


Code Bit : 

import java.util.Scanner;


public class ExampleScannerInput {


public static void main(String[] args) {

// Create Object of Scanner

Scanner sc = new Scanner(System.in);

// Inform the user for input

System.out.println("Enter a input : ");

// hold the input in one string variable

String input = sc.next();

// display the result

System.out.println("The result is :" + input);

}

}

Screenshot image : 

next() method


Result :

next() method in java


                               we saw the next() method code. I have one question if I gave two words separated by space means it will throw an error. Cool yar it will not throw an error. it will print only one word till space. after space, it will not print. That is the next() method specialty. if have curious once try let me know in the comment box. let's go we will see the next Line() method.

Code bit :

import java.util.Scanner;


public class exampleScannerNextLine {


public static void main(String[] args) {

// Create Object of Scanner

Scanner sc = new Scanner(System.in);

// Inform the user for input

System.out.println("Enter a input : ");

// hold the input in one string variable

String input = sc.nextLine();

// display the result

System.out.println("The result is :" + input);


}


}

Screenshot images:


nextLine() method in java

 





Result : 

nextLine method in java





Learned things  :

  • next line() method: it is used to read user input line or sentence. 
  • next() method: it is used for reading words until space.
 
Contribute:  
  • Regarding Job Details Post 
  • Referral Jobs Details
  • Opening for Freshers 
  • Knowledge share with us
Send mail to lifeofsrg@gmail.com. will publish with your name and your content.
 Learn, practice, and share knowledge 

                                                                                                             Thank you :)
                 
                                                                                                      



Tuesday, April 6, 2021

thumbnail

why we need to take user input using scanner

                    Most of time we need to take input from users. why we need to take input from user. is it important ? Yes , it is important because I will explain by one example . let me allow,

           we will write one simple program adding a two numbers. Two numbers are input for  code.  please refer below code snippet. we did hard code like a = 10 and b = 20,  

                  suddenly we need to change means input value. Again go to code and change the variable value. it seems easy for one or two times. n times need to change. it is very difficult to do. So we will take input from user .  user can give any input . code should be work and it should give correct output. That sounds good. so we will use scanner class .

  Code bit : Add two number in Java

Add Two Number in Java
Result






 Scanner class will help us to take input from user. it is present in Javautil  package. 

Let's see code 

In the code, we will take two integer number by user Using Scanner 

Steps :

  • Create scanner object 
  • Take first number  from user (Console) using scanner 
  • Take second number from user (Console)
  • Additional logic 
  • Display the result 


Scanner in Java

import java.util.Scanner;

public class ExampleScanner {

public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   // Taking number from User
       System.out.println("Enter first number : " );
         int a =  sc.nextInt();
       // Taking number from User
       System.out.println("Enter second number : " );
         int b =  sc.nextInt();
        // Adding 
        int c = a + b;
        // Printing number
        System.out.println("The result is :"+ c);
        }
}


Result :



Leant Things : 
  • Scanner Object - it is used for taking user input. 
  • we need to import scanner from java.util.Scanner;
  • Creating scanner object like  Scanner sc = new Scanner(System.in);
  • nextInt() - this method is used for taking int type values from user
we learn only taking int value from user.  we will see upcoming blogs other codes snippet which will take float, double, etc datatypes and string as input  using scanner.



                                      Thank You :)

Sagar R G     
Java Developer 



FullStackDeveloper. Powered by Blogger.