How to Save user Input as a Variable - Scanners!

HOW TO USE SCANNERS!
SKIP TO STEP-BY-STEP VIDEO AT THE BOTTOM IF YOU PREFER
Throughout the course of your programming adventure you will need to use variables that the user will directly enter. For this, you need to use a Scanner. Using it is extremely simple.

For netbeans: In your code, one line above your public class declaration, type import java.util.Scanner;
This loads the scanner code required to utilize the scanner object.

I'll show you working code, then guide you step by step what each line does in the following text.
------------------------------------------------------------------------------------------------------------

package howtouseasscanner;

import java.util.*;
public class Howtouseasscanner {

    public static void main(String[] args) {
     Scanner sc = new Scanner( System.in );
     System.out.println("What is your name?");
     System.out.print("input, please =>");
     String name = sc.nextLine();
     System.out.println("Hello, " +name+ ". Welcome to Scanners!");
           
    }
}
------------------------------------------------------------------------------------------------------------
package howtouseasscanner;
 class YourName {

   public static void main(String[] args) {

   Scanner sc = new Scanner( System.in);
//-Above is our scanner. Read below for a definition.


//  Scanners are declared the same way as other variables, simply typing Scanner sc declares a scanner 
//variable and names it sc. the new Scanner line tells us that the sc variable will come out to be whatever the 
//new Scanner's double parentheses contains, in this case, the new Scanner line's input would be System.in,
//which is the output window below your code. (see video for step by step visual guide.)

System.out.println("What is your name?");
 //  Prints "What is your name? to the output window.
 System.out.print("input, please =>");
//Asks you to write your name!
String name = sc.nextLine();
//This line commands java to create a String under the label 'name', and to assign it a value of the sc scanner, //which will get it's data by the nextLine function, which in this case, will draw it's data from the input that the //user will give in the output window when prompted for their name.
   System.out.println("Hello, " +name+ ". Welcome to Scanners!");
//Prints the value of the string, name!
------------------------------------------------------------------------------------------------------------
STEP BY STEP VIDEO:





5 comments:

  1. My question is: if you ask them to input a double variable, say the price of something, how would you go about formatting the number that they put in to have 2 decimals and a $ symbol before it? I understand how to use the DecimalFormat class, now I just need help on how to implement that to work with the Scanner! Thanks :)

    ReplyDelete
    Replies
    1. import java.text.DecimalFormat;
      import java.util.*;
      public class BlankPaper {
      static DecimalFormat j = new DecimalFormat("$#,###.##");
      public static void main(String[] args) {
      Scanner sc = new Scanner( System.in );
      System.out.println("What's the number you want to format?");
      System.out.println("Input please: -->");
      String number = sc.nextLine();
      double numberParsed = Double.parseDouble(number);
      System.out.println("The formatted number is:"+j.format(numberParsed));



      }

      }

      The reason you see the parse lines is because the scanner in this particular instance can only take a string as the variable type. Well, in order to format it, I have to have a double variable type. So, we use the parse tool to check it for numbers and save them into a new variable, that I then formatted! Check out the video here for another look at the parse tool: http://www.netbeanstutorials.com/p/how-to-save-user-input-as-variable_02.html.

      Delete
  2. Thank you, Taylor. Been searching all morning for the proper protocol to change a scanner to a double that's explained in plain English. You're awesome!

    ReplyDelete
  3. How can we save data in Jtable using vector in netbeans?
    Is this applicable if we're using JtextField and other tools?

    ReplyDelete