This document contains notes on Scanner class methods in Java and examples of using those methods. It discusses the next() and nextLine() methods, differentiating that next() reads the next token up to a delimiter while nextLine() reads the entire line including spaces. It also provides two programming problems and solutions that use Scanner class methods to accept user input and display output.
This document contains notes on Scanner class methods in Java and examples of using those methods. It discusses the next() and nextLine() methods, differentiating that next() reads the next token up to a delimiter while nextLine() reads the entire line including spaces. It also provides two programming problems and solutions that use Scanner class methods to accept user input and display output.
This document contains notes on Scanner class methods in Java and examples of using those methods. It discusses the next() and nextLine() methods, differentiating that next() reads the next token up to a delimiter while nextLine() reads the entire line including spaces. It also provides two programming problems and solutions that use Scanner class methods to accept user input and display output.
This document contains notes on Scanner class methods in Java and examples of using those methods. It discusses the next() and nextLine() methods, differentiating that next() reads the next token up to a delimiter while nextLine() reads the entire line including spaces. It also provides two programming problems and solutions that use Scanner class methods to accept user input and display output.
Notes for class 9 on 10/10/2020 [Scanner class methods and programs]
7.next() This method is used to read the text or a token/a
word (until the first delimiter or space is encountered). It returns a string value. Ex: System.out.println(“Enter a word:”); String st= sc.next ();
8.nextLine() This method is used to read the
complete line of text including spaces. Ex: System.out.println(“Enter a sentence:”); String st= sc. nextLine();
9.next().charAt(0) zero This method is used to read/accept
a character. Ex: System.out.println(“Enter a character:”); char m = sc. next().charAt(0) ; zero
Differentiate between next() and nextLine()
methods. next() nextLine() 1. This method is used to read 1.This method is used to read the text or a token/a word the complete line of text (until the first delimiter or including spaces. space is encountered). It returns a string value.
2. String st= sc.next(); 2. String str=
sc.nextLine(); 1. W A J P TO input you’re your Name, Class ,section and Roll Number and Display them. Sol: import java.util.*; public class scannerclass { public static void main(String hj[]) { Scanner sc= new Scanner(System.in); System.out.println("Enter your name:"); String na=sc.nextLine(); System.out.println("Enter your class:"); byte cl=sc.nextByte(); System.out.println("Enter your section:"); char s=sc.next().charAt(0); System.out.println("Enter your Roll Number:"); short rno=sc.nextShort(); // Displaying the Student's Details System.out.println(); System.out.println("Student\'s Name="+na+"\n class="+cl+"\n Section="+s+"\n Roll no="+rno); } } 2. /* WAJP to accept all the 3 sides of an isoceles triangle's area where area= √s(s-a)s-b)(s-c) where s= a+b+c/2 */ import java.util.*; public class isoscelesarea { public static void main(String jk[]) { Scanner sc= new Scanner(System.in); System.out.println("enter 3 sides of the triangle:"); System.out.println("enter a value:"); short a=sc.nextShort(); System.out.println("enter b value:"); short b=sc.nextShort(); System.out.println("enter c value:"); short c=sc.nextShort(); double s= a+b+c/2.0; System.out.println("The Perimeter="+s); double area = Math.sqrt(s*(s-a)*(s-b)*(s-c)); System.out.println("The Area of an Isosceles Triangle:"+area); } } /* OUTPUT: enter 3 sides of the triangle: enter a value: 5 enter b value: 6 enter c value: 7 The Perimeter=14.5 The Area of an Isosceles Triangle:93.70999146302384 */