Hello,
I'm trying to go through the course at JetBrains Academy (free due to coronavirus!) Until now, it was going better than any other I'd tried.
I know only what it has covered, and there's no system on there for getting help, so...sorry if my questions are really stupid. I'm the idiot who needs help with the tutorial! And yes, I've tried watching videos too. They're great and all but don't answer my questions.
I'm stuck on using the scanner function(s).
It gave this task:
Write a program that reads three strings and outputs them in the reverse order, each in a new line.
I couldn't get anything working so checked the solutions that they have, but I don't understand how the solution works either.
This is a sample solution:
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String word1 = scanner.next(); String word2 = scanner.next(); String word3 = scanner.next(); System.out.println(word3); System.out.println(word2); System.out.println(word1); } }
These are my questions:
Why does the word scanner appear twice in the top line?
What is the word "String" doing at the beginning of those lines? Why is there no definition/value for word1, word2 or word3?
How can it just say scanner.next without having anything to scan? If it does have something to scan, where is it? (and what is it)
When it gets to the output, does it really need three separate lines, and if so, why?
How do I tell it what words to use for words 1, 2 and 3? I was able to give variables values in the previous tasks they gave, but now I'm mixing them with the scanner commands I don't get it at all. I'm getting the message "Variable 'word1' is never used" a lot.
NOW - there are example solutions on there that do use words. They are all written in the code as comments, but I thought if it was a comment, then it wouldn't read it, so that doesn't make sense either?
Thanks...