// Java // make a list reading a txt file import java.io.*; import java.util.*; public class list_from_file_2 { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(new File("list.txt")); List<String> doubles = new ArrayList<String>(); while(scan.hasNext()){ doubles.add(scan.next()); } Collections.sort(doubles); for (String d : doubles) System.out.println(d); } }------------------------------------------------------------------------// Java // make a list reading a txt file import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class list_from_file { public static void main(String[] args) throws IOException { BufferedReader in = null; FileReader fr = null; List<String> list = new ArrayList<String>(); // String type data read from txt file try { fr = new FileReader("list.txt"); in = new BufferedReader(fr); String str; while ((str = in.readLine()) != null) { list.add(str); // or list.setListData(str); // for jList //list.add(Double.parseDouble(str)); // if read data is double number } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); fr.close(); } for (String d : list) System.out.println(d); } }
Showing posts with label java src. Show all posts
Showing posts with label java src. Show all posts
Feb 20, 2012
[Java] make a list reading a txt file
Feb 19, 2012
[Java] array, for-each
// Java // Array class array { public static void main(String[] args) { int[][] arr = new int[3][4]; // 2nd-order array for(int i=0; i<arr.length; i++) for(int j=0; j<arr[i].length; j++) arr[i][j] = i + j; for(int i=0; i<arr.length; i++) { for(int j=0; j<arr[i].length; j++) System.out.print(arr[i][j] + " "); System.out.println(); } int [] ar = {1, 3, 5}; // 1st-order array => int[] ar = new int[] {1,3,5}; for (int e : ar) // for, each System.out.print(e + " "); } }
Feb 16, 2012
[Java] file I/O
// Java // text file read (line by line)import java.io.*; import java.util.*; // to use "ArrayList" public class file_IO_4 { public static void main(String[] args) { // File Open File oFile = new File("./animal/livestock.txt"); // Creat object for File Reader FileReader frd = null; BufferedReader brd = null; // define "ArrayList" for saving line-by-line content of file ArrayList<String> lineList = new ArrayList<String>(); // variables for saving line by line String rLine = null; int lineNum = 0; boolean hasMore = true; try { frd = new FileReader(oFile); brd = new BufferedReader(frd); System.out.println(); while (hasMore) { if((rLine = brd.readLine())!= null){ System.out.println(rLine); // Add read line to ArrayList lineList.add(rLine); lineNum++; hasMore = true; } else hasMore = false; } frd.close(); brd.close(); } catch (IOException e) { e.printStackTrace(); } // Print size of Array List System.out.println(); System.out.println(lineList.size()); System.out.println(lineList); System.out.println(); // Print line by line (for loop) lineNum = lineList.size(); for(int i=0; i<lineNum; i++) { System.out.println( i +": "+lineList.get(i)); } } }---------------------------------------------------------------------// Java // file read and write import java.io.*; // to use file input/output import java.util.*; // to use Scanner (keyboard input) class file_IO_2 { InputStreamReader isr = new InputStreamReader(System.in); BufferedWriter bw = null; BufferedReader br = null; Scanner scan = new Scanner (System.in); int menuNum; String input, output, filename = "sentences.txt"; void p(Object ob) { System.out.print(ob); } public static void main(String[] args) throws Exception { file_IO_2 f2 = new file_IO_2(); f2.menu(); } void menu() throws Exception { p("\n"); p("\t 1. Input sentences \n"); p("\t 2. Print sentences \n"); p("\t 3. Quit \n"); p("\n"); p("\t Select : "); while(true) { if(scan.hasNextInt()) { menuNum = scan.nextInt(); break; } else { p("Please enter number between 1 and 3 \n"); } } switch(menuNum) { case 1: input(); break; case 2: output(); break; case 3: break; default: p("Please enter number between 1 and 3 \n"); menu(); } isr.close(); br.close(); bw.close(); } void input() throws Exception { p("\nLet's start to input sentences \n"); p("If you want to stop, press \"Enter\" in an empty line \n"); bw = new BufferedWriter(new FileWriter(filename)); br = new BufferedReader(isr); while(!(input = br.readLine()).equals("")) { bw.write(input); bw.newLine(); } p("File saved \n"); bw.flush(); menu(); } void output() throws Exception { p("\nSentences in \"" + filename + "\" \n\n"); br = new BufferedReader(new FileReader(filename)); int lineCnt=0; while(true) { lineCnt++; output = br.readLine(); if (output == null) { break; } p(lineCnt + ". " + output + "\n"); } menu(); } }
------------------------------------------------------------------------
// Java // file read and write // 1. input # of items // 2. input item's name, price, location // 3. write input data in a file "item.db" // 4. read the file and print all items' name import java.util.*; import java.io.*; // to use file input, also include "throws IOException" in main method declaration, or "throws Exception" to read file (object in the file) import java.text.DecimalFormat; // to use decimal format class file_IO { public static void main(String[] args) throws Exception { DecimalFormat df = new DecimalFormat ("#.##"); String filename = "item.db"; ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename)); ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename)); // # of items System.out.println("total number of items"); Scanner ni = new Scanner (System.in); while(!ni.hasNextInt()) { ni.next(); // if input is not integer value, ignore it. System.err.println("Input should be integer value"); } int n = ni.nextInt(); // keyboard input for (int nn=1; nn<=n; nn=nn+1) { System.out.println("Input name of item # " +nn); Scanner i1n = new Scanner (System.in); String n1 = i1n.next(); System.out.println("Input price of item # " +nn); Scanner i1p = new Scanner (System.in); while(!i1p.hasNextDouble()) { i1p.next(); // if input is not integer value, ignore it. System.err.println("Input should be a number"); } double p1 = i1p.nextDouble(); System.out.println("Input location of item # " +nn); Scanner i1l = new Scanner (System.in); String l1 = i1l.next(); Item i1 = new Item(n1, p1, l1); out.writeObject(i1); } out.close(); System.out.println(); Item read_object; while( (read_object = (Item) in.readObject()) != null) { System.out.println(read_object.getName()); } in.close(); } } class Item implements Serializable { String name, location; Double price; public Item (String name, double price, String location) { this.name = name; this.price = price; this.location = location; } String getName() { return name; } double getPrice() { return price; } String getLocation() { return location; } }
Feb 15, 2012
[Java] console output (println, print, printf)
\n : next line
\t : tab
\" : " (quotation mark)
\\ : \ (backslash)
System.out.println("She said \"I love you.\" ");
--> She said "I love you."
System.out.print("I will");
System.out.print("get a job soon");
--> I will get a job soon
%d : Integer
%f : Real number
%e : Exponent
%c : Char
%s : String
System.out.printf("%d, %f, %e, %c, %s", 12, 3.44, 0.00223, 'S', "SHSJ");
--> 12, 3.44, 2.23e-3, S, SHSJ
\t : tab
\" : " (quotation mark)
\\ : \ (backslash)
System.out.println("She said \"I love you.\" ");
--> She said "I love you."
System.out.print("I will");
System.out.print("get a job soon");
--> I will get a job soon
%d : Integer
%f : Real number
%e : Exponent
%c : Char
%s : String
System.out.printf("%d, %f, %e, %c, %s", 12, 3.44, 0.00223, 'S', "SHSJ");
--> 12, 3.44, 2.23e-3, S, SHSJ
Feb 14, 2012
[Java] setText format (how to round number)
// Java // how to round number import java.text.DecimalFormat; class round_number { public static void main(String[] args) // main method { DecimalFormat df = new DecimalFormat("#.##"); double num = 3.141592; System.out.println(df.format(num)); } }
-----------------------------------------------------------------
in GUI (Netbeans),
import java.text.DecimalFormat; // outside class //inside method DecimalFormat df = new DecimalFormat("#.##"); jLabel1.setText(df.format(num));
Feb 13, 2012
[Java] method with multiple instances (objects)
method_with_multiple_instances.java
// Java // method with multiple instances (objects) // works with NumberInput.java class method_with_multiple_instances { public static void main(String[] args) // main method { NumberInput n1=new NumberInput(); System.out.println("current value = " + n1.getNum()); simpleMethod(n1); System.out.println("current value = " + n1.getNum()); } public static void simpleMethod(NumberInput calc1) { calc1.addNum(12); } }
-------------------------------------------------------------------
NumberInput.java
// Java // method with multiple instances (objects) // works with method_with_multiple_instances.java class NumberInput { // instance (object) variable int result=0; public void addNum(int n1) // method "addNum" { result = result + n1; } public int getNum() { return result; } }
Feb 12, 2012
[Java] method with reflection
// Java // method with reflection import java.util.*; class method_with_reflection { // A java program must start with "main" method public static void main(String[] args) // main method { System.out.println("first number for factorial?"); Scanner scan1 = new Scanner (System.in); while(!scan1.hasNextDouble()) { scan1.next(); // if input is not integer value, ignore it. System.err.println("Input should be number"); } double n1=scan1.nextDouble(); System.out.println("second number for factorial?"); Scanner scan2 = new Scanner (System.in); while(!scan2.hasNextDouble()) { scan2.next(); // if input is not integer value, ignore it. System.err.println("Input should be number"); } double n2=scan2.nextDouble(); if(n1==0 && n2==0) { bye(); // if weight was input 0, call method "bye" System.exit(0); } System.out.println(); System.out.println(n1 + " factorial = " + factorial(n1)); System.out.println(n2 + " factorial = " + factorial(n2)); } public static double factorial(double n) { if (n==1) return 1; else return n*factorial(n-1); } public static void bye() { System.out.println(); System.out.println("Good Bye"); } }
[Java] method with return values
// Java // method with return values import java.util.*; class method_with_return { // A java program must start with "main" method public static void main(String[] args) // main method { System.out.println("first number?"); Scanner scan1 = new Scanner (System.in); while(!scan1.hasNextDouble()) { scan1.next(); // if input is not integer value, ignore it. System.err.println("Input should be number"); } double n1=scan1.nextDouble(); System.out.println("second number?"); Scanner scan2 = new Scanner (System.in); while(!scan2.hasNextDouble()) { scan2.next(); // if input is not integer value, ignore it. System.err.println("Input should be number"); } double n2=scan2.nextDouble(); if(n1==0 && n2==0) { bye(); // if weight was input 0, call method "bye" System.exit(0); } System.out.println("What do you want? (1) multiply, (2) divide"); Scanner scan3 = new Scanner (System.in); while(!scan3.hasNextInt()) { scan3.next(); // if input is not integer value, ignore it. System.err.println("Input should be 1 or 2"); } int op=scan3.nextInt(); if(op==1) { System.out.println(); System.out.println(n1 + " * " + n2 + " = " + function1(n1, n2)); } else { System.out.println(); System.out.println(n1 + " / " + n2 + " = " + function2(n1, n2)); } } public static double function1(double n1, double n2) { return n1*n2; } public static double function2(double n1, double n2) { return n1/n2; } public static void bye() { System.out.println(); System.out.println("Good Bye"); } }
[Java] method without return value
// Java // method import java.util.*; class method { // A java program starts with "main" method public static void main(String[] args) // main method { System.out.println("What's your weight in kilogram?"); Scanner scan1 = new Scanner (System.in); while(!scan1.hasNextDouble()) { scan1.next(); // if input is not integer value, ignore it. System.err.println("Input should be number"); } double weight=scan1.nextDouble(); System.out.println("What's your height in centimeter?"); Scanner scan2 = new Scanner (System.in); while(!scan2.hasNextDouble()) { scan2.next(); // if input is not integer value, ignore it. System.err.println("Input should be number"); } double height=scan2.nextDouble(); if(weight==0) { bye(); // if weight was input 0, call method "bye" System.exit(0); } function1(weight, height); // give "weight" and "height" values to method "fuction1" } public static void function1(double weight, double height) { System.out.println(); System.out.println("Calories you daily need = "+(int)(weight*24)); System.out.println(); System.out.println("Recommended weight for you = " + (int)(height-110) + " (female)"); System.out.println("Recommended weight for you = " + (int)(height-100) + " (male)"); } public static void bye() { System.out.println(); System.out.println("Good Bye"); } }
Subscribe to:
Posts (Atom)