Presentation is loading. Please wait.

Presentation is loading. Please wait.

Taking Input Java Md. Eftakhairul Islam

Similar presentations


Presentation on theme: "Taking Input Java Md. Eftakhairul Islam"— Presentation transcript:

1 Taking Input Java Md. Eftakhairul Islam

2 Scanner Class The Scanner class is a class in java.util, which allows the user to read values of various types by its method. The Scanner class is a class in java.util, which allows the user to read values of various types by its method. In Java documentation Link: 2

3 Scanner Class Methods BRAC University Computer Club 3 MethodReturns 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 //Import the class import java.util.Scanner;public class NumericInput{ public static void main(String[] args){ public static void main(String[] args){ // 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 long integer =+ longInteger +,a floating-point = + doubleReal+,and a string = + stringStoken+, It will print whole line = + stringLine); System.out.println("Enter an integer = + integer+, a long integer =+ longInteger +,a floating-point = + doubleReal+,and a string = + stringStoken+, It will print whole line = + stringLine); } } 4 Scanner Class Example

5 5 BufferedReader import java.io.*; Public class Taking input { Public static void main(String [] args) throws IOException Public static void main(String [] args) throws IOException { { BufferedReader x = new BufferedReader(new InputStreamReader(System.in)); BufferedReader x = new BufferedReader(new InputStreamReader(System.in)); // String box; //String box=x.readLine(); Converting x in Integer int n=Integer.parseInt( x.readLine());

6 6 //Converting x in Float float f=Float.parseFloat(x.readLine()); float f=Float.parseFloat(x.readLine()); //Converting x in Double double d=Double.parseDouble(x.readLine()); double d=Double.parseDouble(x.readLine()); //Converting x in Long long l=Long.parseLong(x.readLine()); long l=Long.parseLong(x.readLine()); //Converting x in Byte byte b=Byte.parseByte(x.readLine()); byte b=Byte.parseByte(x.readLine()); }}

7 7 JOptionPane //importing class from java Import javax.swing.JOptionPane.*; Main()……… String s = JOptionPane.showInputDialog ("Enter any number"); //Converting x in integer int p = Integer.parseInt(x); //Converting x in Double double d = Double.parseDouble(x.readLine());

8 Take input from file // Declarations Scanner inFile = new Scanner(new FileReader("File name with directories")); //Read token by token from file String stringStoken= inFile.next(); //Read Line by Line from file String stringLine = inFile.nextLine(); 8

9 Reading 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(new InputStreamReader(urlConn.getInputStream())); BufferedReader buff= new BufferedReader(new 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()); } } } } } } 9

10 10 Difference between println & print System.out.println(java);System.out.println(world);>Javaworld> (Different line ) (Different line )

11 11 System.out.print() System.out.print(java);System.out.print(world);> javaworld> javaworld> (Same line) (Same line)

12 12 out.println( ); import static java.lang.System.out; main()……. out.println(shortcut of println); out.println(shortcut of println); out.print(shortcut of print); out.print(shortcut of print);

13 13 ….Thank You….


Download ppt "Taking Input Java Md. Eftakhairul Islam"

Similar presentations


Ads by Google