Java experiment 7 stream

Source: Internet
Author: User
Experiment 7 streams (2 hours) I. Purpose

1. Familiar with the usage of various common streams in the stream class library.

2. You can use stream classes to perform basic file read/write operations.

Ii. experiment content

1. Write a program, enter two integers in the console window, then receive the two integers, and output their sum. (Requirement: the keyboard input is obtained through the stream encapsulation system. In. Do not use the keyboard class)

2. design student for student. attributes: Number (integer), name (string), and score (integer ). Write a program: Requirement: (1) input the names and tokens of three students, save their names and tokens to data.txt; (2) then read data from the file to obtain the average score of the five students.

Iii. Lab requirements

Complete the program design and submit the lab report.

 

package Sum;import java.io.*;public class Sum {public static void main(String args[]) {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));    String s1,s2;    int num1 = 0,num2 = 0;    try{    s1=br.readLine();num1=Integer.parseInt(s1);s2=br.readLine();num2=Integer.parseInt(s2);}catch (IOException e){e.printStackTrace();}    System.out.println(num1+"+"+num2+"="+(num1+num2)); }}

 

 

Package writetofile; import Java. io. *; public class student {public void input () {bufferedreader BR = new bufferedreader (New inputstreamreader (system. in); For (INT I = 0; I <3; I ++) {string SnO = NULL, sdegree = NULL, name = NULL; try {system. out. println ("Enter the student ID" + (I + 1) + ":"); SnO = BR. readline (); system. out. println ("input number" + (I + 1) + "Student name:"); name = BR. readline (); system. out. println ("input number" + (I + 1) + "Student Score:"); sdeg REE = BR. readline () ;}catch (ioexception E1) {e1.printstacktrace () ;}try {filewriter fw = new filewriter ("data.txt", true); bufferedwriter BW = new bufferedwriter (FW ); BW. write (SNO + "" + name + "" + sdegree); BW. newline (); BW. close (); FW. close ();} catch (ioexception e) {e. printstacktrace () ;}} public double average () {string STR = NULL; double AVG = 0; string [] [] temp = new string [3] [3]; filereader fr; Try {Fr = new filereader ("data.txt"); bufferedreader BR = new bufferedreader (FR); For (INT I = 0; (STR = Br. Readline ())! = NULL; I ++) {temp [I] = Str. split (""); AVG + = double. parsedouble (temp [I] [2]);} system. out. println ("3 average scores:" + AVG/3);} catch (exception e) {e. printstacktrace ();} return 0;} public static void main (string [] ARGs) throws ioexception {student s = new student (); S. input (); S. average ();}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.