Taking User Input in Java


Published on

This is one of my old slide. I took an one day class in BRAC University in 2007 on taking input in Java programming language. This tutorial is for begginer studemts.

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Taking User Input in Java

  1. 1. Taking InputTaking Input JavaJava Md. Eftakhairul IslamMd. Eftakhairul Islam eftakhairul@gmail.comeftakhairul@gmail.com http://eftakhairul.wordpress.comhttp://eftakhairul.wordpress.com
  2. 2. Scanner ClassScanner Class The Scanner class is a class inThe Scanner class is a class in java.util, which allows the user tojava.util, which allows the user to read values of various types by itsread values of various types by its method.method. In Java documentationIn Java documentation Link:Link: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html 22http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  3. 3. Scanner Class MethodsScanner Class Methods BRAC University Computer ClubBRAC University Computer Club 33 Method Returns int nextInt() Returns the next token as an int. long nextLong() Returns the next token as a long float nextFloat() Returns the next token as a float. double nextDouble() Returns the next token as a long String next() Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break String nextLine() Returns the rest of the current line, excluding any line separator at the end.
  4. 4. //Import the class//Import the class import java.util.Scanner;import java.util.Scanner; public class NumericInput{public class NumericInput{ public static void main(String[] args){public static void main(String[] args){ // Declarations// Declarations Scanner in = new Scanner(System.in);Scanner in = new Scanner(System.in); int integer = in.nextInt();int integer = in.nextInt(); long longInteger = in.nextLong();long longInteger = in.nextLong(); float realNumber = in.nextFloat();float realNumber = in.nextFloat(); double doubleReal = in.nextDouble();double doubleReal = in.nextDouble(); String stringLine = in.nextLine();String stringLine = in.nextLine(); String stringStoken= in.next();String stringStoken= in.next(); System.out.println("Enter an integer = “+ integer+”, a longSystem.out.println("Enter an integer = “+ integer+”, a long integer =“+ longInteger +“,a floating-point = “+ doubleReal+”integer =“+ longInteger +“,a floating-point = “+ doubleReal+” ,and a string = “+ stringStoken+”, It will print whole line = “+,and a string = “+ stringStoken+”, It will print whole line = “+ stringLine);stringLine); } }} } 44http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com Scanner Class ExampleScanner Class Example
  5. 5. 55 BufferedReaderBufferedReader import java.io.*;import java.io.*; Public class Taking inputPublic class Taking input {{ Public static void main(String [] args)Public static void main(String [] args) throws IOExceptionthrows IOException {{ BufferedReader x = new BufferedReader(newBufferedReader x = new BufferedReader(new InputStreamReader(System.in));InputStreamReader(System.in)); //// String box;String box; //String box=x.readLine();//String box=x.readLine(); Converting x in IntegerConverting x in Integer int n=Integer.parseInt(int n=Integer.parseInt(xx.readLine());.readLine()); http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  6. 6. 66 //Converting x in Float//Converting x in Float float f=Float.parseFloat(x.readLine());float f=Float.parseFloat(x.readLine()); //Converting x in Double//Converting x in Double double d=Double.parseDouble(x.readLine());double d=Double.parseDouble(x.readLine()); //Converting x in Long//Converting x in Long long l=Long.parseLong(x.readLine());long l=Long.parseLong(x.readLine()); //Converting x in Byte//Converting x in Byte byte b=Byte.parseByte(x.readLine());byte b=Byte.parseByte(x.readLine()); }} }} http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  7. 7. 77 JOptionPaneJOptionPane //importing class from java//importing class from java Import javax.swing.JOptionPane.*;Import javax.swing.JOptionPane.*; Main()………Main()……… String s = JOptionPane.showInputDialog ("String s = JOptionPane.showInputDialog ("Enter anyEnter any numbernumber");"); //Converting x in integer//Converting x in integer int p = Integer.parseInt(x);int p = Integer.parseInt(x); //Converting x in Double//Converting x in Double double d = Double.parseDouble(x.readLine());double d = Double.parseDouble(x.readLine()); http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  8. 8. Take input from fileTake input from file // Declarations// Declarations Scanner inFile = new Scanner(newScanner inFile = new Scanner(new FileReader("File name with directories"));FileReader("File name with directories")); //Read token by token from file//Read token by token from file String stringStoken= inFile.next();String stringStoken= inFile.next(); //Read Line by Line from file//Read Line by Line from file String stringLine = inFile.nextLine();String stringLine = inFile.nextLine(); 88http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  9. 9. Reading data from InternetReading data from Internet  import java.net.*;import java.net.*;  import java.io.*;import java.io.*;  public class WebSiteReader {public class WebSiteReader {  public static void main(String args[]){public static void main(String args[]){  String nextLine;String nextLine;  try{try{  // Create the URL obect that points & at the default file index.html// Create the URL obect that points & at the default file index.html  URL url = new URL("http://rainbd.freelance.com.bd/index.html" );URL url = new URL("http://rainbd.freelance.com.bd/index.html" );  URLConnection urlConn = url.openConnection();URLConnection urlConn = url.openConnection();  BufferedReader buff= new BufferedReader(newBufferedReader buff= new BufferedReader(new InputStreamReader(urlConn.getInputStream()));InputStreamReader(urlConn.getInputStream()));  // Read and print the lines from index.html// Read and print the lines from index.html  while (true){while (true){  nextLine =buff.readLine();nextLine =buff.readLine();  if (nextLine !=null){if (nextLine !=null){  System.out.println(nextLine);System.out.println(nextLine);  }else{}else{  break;break;  }}  }}  } catch(Exception e){} catch(Exception e){  System.out.println("Please read the exception carefully:" + e.toString());System.out.println("Please read the exception carefully:" + e.toString());  } } }} } } http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com 99
  10. 10. 1010 Difference between println & printDifference between println & print System.out.println(“java”);System.out.println(“java”); System.out.println(“world”);System.out.println(“world”); >> JavaJava worldworld >> (Different line )(Different line ) http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  11. 11. 1111 System.out.print()System.out.print() System.out.print(“java”);System.out.print(“java”); System.out.print(“world”);System.out.print(“world”); >> javaworld>javaworld> (Same line)(Same line) http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  12. 12. 1212 out.println( );out.println( ); import static java.lang.System.out;import static java.lang.System.out; main()…….main()……. out.println(“shortcut of println”);out.println(“shortcut of println”); out.print(“shortcut of print”);out.print(“shortcut of print”); http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  13. 13. 1313 …….Thank You…..Thank You…. http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com