Wednesday, November 7, 2012
Scanner in Java
package com.scanner; import java.util.Scanner; /** * * @author Thanooj K * */ class ConsoleMe { static Scanner sc = new Scanner(System.in); public static EnterMe askYorN(String prompt) { while (true) { String input; System.out.print("\n" + prompt + " (Y or N) "); input = sc.next(); if (input.equalsIgnoreCase("Y")) return new EnterMe("Y", true); else if (input.equalsIgnoreCase("N")) return new EnterMe("N", false); ; } } } class EnterMe { private String yOrN; private boolean status; public EnterMe(String yOrN, boolean status) { super(); this.yOrN = yOrN; this.status = status; } public boolean isStatus() { return status; } public String getyOrN() { return yOrN; } } public class ScannerMe { public static void main(String[] args) { EnterMe enterMe = ConsoleMe.askYorN("Keep going?"); if (enterMe != null && enterMe.isStatus()) { System.out.println("You have chosen :: " + enterMe.getyOrN() + " ," + enterMe.isStatus()); } else { System.out.println("You have chosen :: " + enterMe.getyOrN() + " ," + enterMe.isStatus()); } } }
output:
Keep going? (Y or N) :: Y
You have chosen :: Y ,true
------------------------------------------------package com.scanner; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; class ConsoleMeForSearchText { public void consoleMeForSearchText(Path dirPath) { DirectoryStream < Path > dirStream = null; try { // 2. Create a DirectoryStream instance using // Files.newDirectoryStream(). The instance // is created by passing the Path of the directory // to be searched and the type of files as a filter // here we are using filter to search all pdf files // in the directory dirStream = Files.newDirectoryStream(dirPath, "*.pdf"); // 3. Looping each file and printing the name // on the console for (Path entry : dirStream) { System.out.println(entry.getParent() + "\\" +entry.getFileName()); } } catch (IOException e) { System.out.println(e.getMessage()); } finally { // 4. Closing the DirectoryStream try { dirStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public class SearchFilesInDirectory { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { String dirPathStr = sc.next(); Path dirPath = Paths.get(dirPathStr); ConsoleMeForSearchText consoleMeForSearchText = new ConsoleMeForSearchText(); consoleMeForSearchText.consoleMeForSearchText(dirPath); } }output: (give the folder path to search for all PDFs)
C:\Users\Thanooj\Downloads\desktopNotes C:\Users\Thanooj\Downloads\desktopNotes\A-Business-Case-for-Ajax-with-GWT-JAOO-2006.pdf C:\Users\Thanooj\Downloads\desktopNotes\getstartderby_JavaDB.pdf C:\Users\Thanooj\Downloads\desktopNotes\GWT in Practice.pdf C:\Users\Thanooj\Downloads\desktopNotes\head_first_sql.pdf C:\Users\Thanooj\Downloads\desktopNotes\Manning_GWT_in_Action.pdf C:\Users\Thanooj\Downloads\desktopNotes\microsoft_sql_server_2008_high_availability.pdf C:\Users\Thanooj\Downloads\desktopNotes\MR. K THANOOJ-12032013 - 11042013.pdf C:\Users\Thanooj\Downloads\desktopNotes\Prentice.Hall.Google.Web.Toolkit.Applications.Dec.2007.pdf C:\Users\Thanooj\Downloads\desktopNotes\Pro Web 2.0 - Application Development with GWT.pdf C:\Users\Thanooj\Downloads\desktopNotes\professional_microsoft_sql_server_2008_programming.pdf C:\Users\Thanooj\Downloads\desktopNotes\sams_teach_yourself_sql_in_24_hours_5th_edition.pdf C:\Users\Thanooj\Downloads\desktopNotes\SQL Server Essential Guide FINAL.pdf C:\Users\Thanooj\Downloads\desktopNotes\sql_pocket_guide_third_edition.pdf C:\Users\Thanooj\Downloads\desktopNotes\Writing-Big-Apps-with-GWT-TSSJS-Vegas-2007.pdf
Enum in Java
package com; interface MyEnum { public abstract String inString(); } public class MyMonth { public enum Months implements MyEnum { JAN(1) { @Override public String inString() { return "JANUARY"; } }, FEB(2) { @Override public String inString() { return "FEBRUARY"; } }, MAR(3) { @Override public String inString() { return "MARCH"; } }, ARL(4) { @Override public String inString() { return "APRIL"; } }; private int value; private Months(int value) { this.value = value; } public int getValue() { return value; } }; public static void main(String[] args) { for (MyMonth.Months month : MyMonth.Months.values()) { System.out.println("month:- " + month + " :: " + month.getValue() + " :: " + month.inString()); } switch (MyMonth.Months.ARL) { case JAN: System.out.println("JANUARY"); break; case FEB: System.out.println("FEBRUARY"); break; case MAR: System.out.println("MARCH"); break; case ARL: System.out.println("APRIL"); } } }
OUTPUT---------
Subscribe to:
Posts (Atom)