Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent method to C's scanf in Java?

Tags:

java

Java has the notion of format strings, bearing a strong resemblance to format strings in other languages. It is used in JDK methods like String#format() for output conversion.

I was wondering if there's an input conversion method akin to C's scanf in Java?

like image 281
Gopichand Avatar asked Jun 07 '13 09:06

Gopichand


People also ask

What does scanf mean in Java?

What Does Scanf Mean? In the C programming language, scanf is a function that reads formatted data from stdin (i.e, the standard input stream, which is usually the keyboard, unless redirected) and then writes the results into the arguments given.

How do we take input in Java?

The Scanner class is used to get user input, and it is found in the java.util package.

What does printf and scanf do in Java?

The scanf() function allows us to read one or multiple values entered by the user through the keyboard at the console. The printf() function is used to print or display one or multiple values in the output to the user at the console.

What is system in in Java?

System.in is an InputStream which is typically connected to keyboard input of console programs. System.in is not used as often since data is commonly passed to a command line Java application via command line arguments, or configuration files. In applications with GUI the input to the application is given via the GUI.


1 Answers

Take a look at this site, it explains two methods for reading from console in java, using Scanner or the classical InputStreamReader from System.in.

Following code is taken from cited website:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;  public class ReadConsoleSystem {   public static void main(String[] args) {      System.out.println("Enter something here : ");      try{         BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));         String s = bufferRead.readLine();          System.out.println(s);     }     catch(IOException e)     {         e.printStackTrace();     }    } } 

--

import java.util.Scanner;  public class ReadConsoleScanner {   public static void main(String[] args) {        System.out.println("Enter something here : ");         String sWhatever;         Scanner scanIn = new Scanner(System.in);        sWhatever = scanIn.nextLine();         scanIn.close();                    System.out.println(sWhatever);   } } 

Regards.

like image 173
wizard Avatar answered Sep 21 '22 04:09

wizard