Wikipedia:Reference desk/Archives/Science/2018 November 11

Science desk
< November 10 << Oct | November | Dec >> November 12 >
Welcome to the Wikipedia Science Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


November 11

edit

Java Programming

edit

For java programming, what is the difference between read() and next()?

For example:

Let's say I import the java Scanner class. Then I proceed as follows:

Scanner userInput = new Scanner(System.in); System.out.print("Enter the number of students: "); int numberOfStudents = userInput.nextInt(); System.out.println("The number of students you entered is: " + numberOfStudents);

My question is would this work if I replaced "int numberOfStudents = userInput.nextInt()" with "int numberOfStudents = userInput.readInt()"? What is the difference between next and read? When should one be used over the other?

ThunderBuggy (talk) 17:20, 11 November 2018 (UTC)[reply]

According to the documentation Scanner class does not have readInt method. Ruslik_Zero 20:27, 11 November 2018 (UTC)[reply]
So I would have had to create a readInt method before using it then, right? And the "next" methods are included with the Scanner class? ThunderBuggy (talk) 03:29, 12 November 2018 (UTC)[reply]
This isn't a general Java language question but, like nearly all Java questions, a question about different libraries used by Java. As Java has been around a while, there are many such libraries and they're often inconsistent. If we started again, we wouldn't have so many of these variations.
For the Scanner library class,[1] this implements and extends the Iterator<String> classinterface [2], which defines a next() method.
What Iterator does is that it allows next() to read, identify and move past a series of "tokens" in the input stream. That means you can write an input loop for a long series of tokens with just a simple loop. Then Scanner extends this, so that it recognises different types of token, such as nextInt() to read integers.
Don't mess with next() etc. by trying to rename or facade them as something else. Otherwise you lose the advantage of using Iterator in a consistent and clearly understood way. There is no readInt() in Scanner or Iterator.
readInt() comes instead from DataInputStream[3] rather than Scanner. This is part of java.io rather than java.util.
Now we see the crucial difference! java.util is for chopping up bits of well-behaved data which are already loaded into memory. java.io though is for dealing with the nasty real world of outside interfaces, data sources which stop halfway or timeout.
So for what you're doing here, then yes, base it on DataInputStream and readInt(), rather than Scanner and nextInt(). You're doing this because of the purposes of the different classes (DataInputStream understands real world IO), not simply the different methods. Andy Dingley (talk) 13:55, 12 November 2018 (UTC)[reply]
Iterator is an interface, not class. So, Scanner class implements it and extends Object class. Ruslik_Zero 20:43, 12 November 2018 (UTC)[reply]

Fat and the blood-brain barrier

edit

According to Wikipedia the human brain is nearly 60 percent fat. Also according to Wikipeida fat cannot cross the blood–brain barrier, hence the need for the liver to convert fat into ketone bodies which can then cross the blood–brain barrier and nourish the brain during periods of starvation. "Unlike free fatty acids, ketone bodies can cross the blood-brain barrier". So explain to me how the brain came to be 60 percent fat if fat cannot reach the brain due to the blood–brain barrier. 200.122.209.78 (talk) 19:51, 11 November 2018 (UTC)[reply]

See Lipogenesis. There's a reason you can get fat even if most of your energy comes from carbohydrates (such as sugars or starch) or protein and not fat. Nil Einne (talk) 20:35, 11 November 2018 (UTC)[reply]