Question about this code

Put your programming skills to the test in these challenges.

Question about this code

Post by ghostheadx2 on Wed May 27, 2015 3:02 pm
([msg=88212]see Question about this code[/msg])

So, I've been doing tutorials in Java to go beyond what my teacher taught me in class. I have a question about this code:

Code: Select all
import java.util.Scanner;

class apples{
   public static void main(String args[]){
      Scanner bucky = new Scanner(System.in);
      System.out.println(bucky.nextLine());
   }
}


Alright, I get it. the scanner is imported. Then, whatever is assigned to the scanner variable is printed on the next line. Why is the extra .nextLine() important if println is already there? It seems redundant. Lol. :?:
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: Question about this code

Post by cyberdrain on Fri Jun 05, 2015 6:10 am
([msg=88341]see Re: Question about this code[/msg])

A quick search confirms my suspicions: it has something to do with newlines. Go look for it :)
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Question about this code

Post by pretentious on Fri Jun 05, 2015 9:53 am
([msg=88347]see Re: Question about this code[/msg])

System.in is standard input (from your keyboard)
The program waits for your input and then writes it to the Stdin buffer
You ask for the next available line from scanner and it reads the line you just typed
It's then printed my the println statement

I don't understand your question. Go look up the java api
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1203
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: Question about this code

Post by sanddbox on Fri Jun 05, 2015 3:19 pm
([msg=88350]see Re: Question about this code[/msg])

He's confused because "println" and Scanner.nextLine() look similar.

The answer is that they're two different things. System.out.println() is used to output anything to the console, and the Scanner.nextLine() is a method to read the next input line. They look similar but they do entirely separate things.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2344
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron