Java Programming Examples Tutorial Index

Java String Programs

This Java program is used to print on the screen input by the user.



This Java program asks the user to provide a string, integer and float input, and prints it.

  • Scanner class and its functions are used to obtain inputs, and println() function is used to print on the screen.
  • Scanner class is a part of java.util package, so we required to import this package in our Java program.
  • We also required to create a object of Scanner class to call its functions.
  • There are different functions is available to obtain integer, float and string inputs:
    nextInt() function for integer input,  nextFloat() function for float input and nextLine() function for string input.

Example:

import java.util.Scanner;
class GetInputs
{
   public static void main(String args[])
   {
      int a;
      float b;
      String s;
      Scanner obj = new Scanner(System.in); /* create a object */
 
      System.out.println("Enter a string:");
      s = obj.nextLine(); /* Take string input and assign to variable */
      System.out.println("You entered string "+s); /* Print */
 
      System.out.println("Enter an integer:");
      a = obj.nextInt(); /* Take integer input and assign to variable */
      System.out.println("You entered integer "+a); /* Print */
 
      System.out.println("Enter a float:");
      b = obj.nextFloat(); /* Take float input and assign to variable */
      System.out.println("You entered float "+b); /* Print */
   }
}

Program Output:

Enter a string:
w3schools
You entered string w3schools

Enter an integer:
15 
You entered integer 15

Enter a float:
12.5
You entered float 12.5


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram