Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input Output Examples. import java.io.*; class Greetings { public static void main (String[] args) { try { DataInputStream in = new DataInputStream(System.in);

Similar presentations


Presentation on theme: "Input Output Examples. import java.io.*; class Greetings { public static void main (String[] args) { try { DataInputStream in = new DataInputStream(System.in);"— Presentation transcript:

1 Input Output Examples

2 import java.io.*; class Greetings { public static void main (String[] args) { try { DataInputStream in = new DataInputStream(System.in); System.out.println("What is your name?"); String name = in.readLine(); System.out.println("Hello " + name); } catch (IOException e) { System.out.println("Exception: " + e.getMessage()); } /////////////////////////////////////////////////////////////////////////////// What is your name? Bill Gates Hello Bill Gates

3 import java.io.*; class Sum { public static void main (String[] args) { try { DataInputStream in = new DataInputStream(System.in); DataInputStream fin = new DataInputStream(new FileInputStream(“numbers.dat”)); int count; double total = 0; System.out.print(“How many numbers? “); System.out.flush(); count = Integer.parseInt(in.readLine()); for (int i=1; i<=count; i++) { Double number = Double.valueOf(fin.readLine()); total += number.doubleValue(); } System.out.println(“The total is: “ + total); } catch (IOException e) { System.out.println(“Exception while performing “ + e.getMessage()); }

4 “Numbers.dat” file 1.1 2.2 3.3 4.4 10.5 ///////////////////////////////////////// javac Sum.java java Sum How many numbers: 5 The total is 21.5

5 //This program does not use deprecated methods import java.io.*; class MyTest { BufferedReader reader = null; public void read() { try { reader = new BufferedReader (new FileReader ("numbers.dat")); } catch (FileNotFoundException f)//if file was not found { System.out.println("File was not found"); System.exit(0); }

6 try { String line= new String(); double sum = 0.0; while((line=reader.readLine())!=null) { double d = Double.parseDouble(line); sum += d; } System.out.println("Sum is " + sum); } catch (Exception e) { System.out.println("Exception occurred"); } } // close read() function public static void main(String[] args) { MyTest test = new MyTest(); test.read(); } } // close MyTest class

7 Class java.io.File import java.io.*; public class BuildDir { public static void main(String[] args) throws IOException { File from = new File("source.txt"); File newDir = new File("newDir"); File to = new File("newDir/target.txt"); newDir.mkdir(); FileReader in = new FileReader( from ); FileWriter out = new FileWriter( to ); int character; while( (character=in.read())!= -1 ) { out.write(character); } in.close(); out.close(); from.delete(); }

8 Useful methods of File getAbsoulutePath() – return string. Absoulute path of the file. canRead(),canWrite()-return boolean.app can read/write to file. IsFile(), isDirectory()- return boolean. list()- return string[]. The list of the files in the directory. mkDir() – return boolean. Creat a directory. renameTo(File des) –return boolean. Renames the file name to the Des pathname.

9 StringTokenizer StringTokenizer היא מחלקה שמקבלת מחרוזת ומפרקת אותה לתתי מחרוזות תוך התעלמות מרווחים וטאבים (פירסינג). ;(StringTokenizer st = new StringTokenizer (str, delim, true הסבר: העצם st מקבל 3 פרמטרים: str - מחרוזת לפירוק delim - מפרידים. למשל: "," ";" וכיו"ב. כלומר, המחרוזת str תחולק לחלקים, תתי מחרוזות, עפ"י המפריד שנבחר (אם בחרנו פסיק, המחלקה תחלק את המחרוזת עפ"י הפסיקים שיש בה). ניתן להגדיר כמה מפרידים ואז הם יכתבו ברצף בתור הפרמטר delim. למשל: ";,." יגרום לחלוקת המחרוזת str עפ"י פסיקים, נקודה-פסיק, או נקודה. true/false - ערך true יחזיר גם את המפרידים (כלומר את הפסיק למשל) בתור תת מחרוזת. פונקציות במחלקה ( )st.nextToken - תחזיר את חלק המחרוזת הבא. היא יכולה לקבל מפריד מסוים, ואז תחלק את החלק הבא רק עפ"י המפריד הספציפי, ולא עפ"י המפרידים שהועברו בבנאי. ( )st.hasMoreToken - מחזיר true/false אם הסתיימה חלוקת המחרוזת.

10 java.util.StringTokenizer import java.io.*; import java.util.StringTokenizer; public class Tokens { public static void main(String[] args) throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int first,second,pitaron; int i; char sign; String line; do { System.out.println("Enter the exercise with =."); line = in.readLine(); StringTokenizer st=new StringTokenizer(line); first=Integer.parseInt( st.nextToken("-+*/") ); sign = ( st.nextToken("1234567890") ).charAt(0); second= Integer.parseInt( st.nextToken("=") );

11 switch(sign) { case '+': pitaron= first+second; break; case '-': pitaron= first-second; break; case '*': pitaron= first*second; break; case '/': pitaron= first/second; break; default : pitaron =0; } System.out.println(line + pitaron); } while( pitaron != 0); } // close main } //close class

12 output: Enter the exercise with =. 12-33= 12-33=-21 StringTokenizer(st1,delim)- construct a StringTokenizer for st1. delim= the delimiters. StringTokenizer(st1)- construct a StringTokenizer for st1. delimiters= tab,\n,space.(default) nextToken(delim)- return the string until the delim. CountTokens()- return the number of tokens, using the current delimiter set. HasMoreTokens()- return boolean, test if there are more tokens available.


Download ppt "Input Output Examples. import java.io.*; class Greetings { public static void main (String[] args) { try { DataInputStream in = new DataInputStream(System.in);"

Similar presentations


Ads by Google