In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the concept of Java stream". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the concept of Java stream.
Yesterday, my dorm classmate asked me a question about Java stream. After answering it, I had a lot of feelings. I think all beginners may have such questions, so sum up and send them out. For your reference:
Well, the question asked by my classmate, in fact, I didn't quite understand what it meant, because he didn't invite me, but I was clear about one thing, that is, he had to enter the number from the keyboard (the kind of console) and then calculate the area of the rectangle.
A problem similar to this one. Oh, in fact, the problem is very simple, in C++, just cin on the ok, but in java, it is not so simple, he originally used System.in.read (); but this function into the return is the int shape, but in fact, the ASCII code, such as input 1, then the return is 51 (can not remember the ASC code of 1), not 1, of course you can give him to convert into char but char has not turned int.
In short, it was troublesome, and I didn't know, so I took a different approach. So I changed the method, this method is as follows, I think there are probably many newcomers are not very good, so the source code is as follows:
Package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class TestInOut {/ * * @ param args * / public static void main (String [] args) {/ / TODO automatic generation method stub Test test=new Test (); BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); System.out.print ("Please enter the length of the rectangle:") Try {test.setA (Float.parseFloat (stdin.readLine (); System.out.print ("Please enter the width of the rectangle:"); test.setB (Float.parseFloat (stdin.readLine (); test.setArea (); System.out.print ("the area of the rectangle is:"); System.out.println (test.getArea ());} catch (IOException e) {/ / TODO automatically generates catch blocks e.printStackTrace () } class Test {private float a; private float b; public float getB () {return b;} public void setB (float b) {this.b = b;} public void setA (float a) {this.a=a;} public float getA () {return a;} private float area; public float getArea () {return area;} public void setArea () {area=a*b }}
Execution result:
Please enter the length of the rectangle: 3
Please enter the width of the rectangle: 4
The area of the rectangle is: 12.0
The BufferedReader in this is a stream, hehe, so I summarized the stream by the way, please take a look at the following code:
Package test; import java.io.*; public class IOStreamDemo {public void samples () throws IOException {/ / 1. This reads a line of data from the keyboard and returns the string BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); System.out.print ("Enter a line:"); System.out.println (stdin.readLine ()); / / 2. This reads BufferedReader in = new BufferedReader (new FileReader ("IOStreamDemo.java")) line by line from the file; String s, S2 = new String (); while ((s = in.readLine ())! = null) S2 + = s + "\ n"; in.close (); / / 3. This reads the bytes StringReader in1 = new StringReader (S2); int c; while ((c = in1.read ())! =-1) System.out.print ((char) c) one by one from a string. This is to write a string to the file try {BufferedReader in2 = new BufferedReader (new StringReader (S2)); PrintWriter out1 = new PrintWriter (new BufferedWriter (new FileWriter ("IODemo.out"); int lineCount = 1; while ((s = in2.readLine ())! = null) out1.println (lineCount++ + ":" + s); out1.close ();} catch (EOFException e) {System.err.println ("End of stream");}
I think after looking at the above code, you should have a simple understanding that you can use it to read data from the keyboard.
At this point, I believe you have a deeper understanding of "how to understand the concept of Java flow". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.