Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Super basic questions

  1. #1
    Junior Member
    Join Date
    May 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Super basic questions

    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...
    Last edited by Amanita; May 4th, 2020 at 12:05 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Super basic questions

    Why does the word scanner appear twice in the top line?
    Which line is the top line? It might be clearer if a comment were added to the line your are asking about.
    I do not see any line where scanner appears twice. Case is important in java. scanner is not the same as Scanner
    Scanner is the name of a class. scanner is the name of a variable that refers to a Scanner object.

    What is the word "String" doing at the beginning of those lines
    String is the name of a class. Those line are to declare some variables that refer to String objects.

    See the tutorial for more info: https://docs.oracle.com/javase/tutor...ybigindex.html
    https://docs.oracle.com/javase/tutor...variables.html

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Super basic questions

    ^ Thanks. I get that String is the name of a class but, in that example, as far as I can see it is NOT defining any variables? There is no definition after the '=' sign so....what?

    String word1 = hello

    would be declaring that 'word1' is 'hello', right? but there isn't a value in the example.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Super basic questions

    String word1 = hello
    What is hello? A String variable or is it supposed to be a String? If so it should be enclosed in "s: "hello"
    Also statements should end with a ;

    defining any variables
    The above statement defines/declares a variable named word1 of type String

    there isn't a value in the example.
    There are 4 variables defined in the original code: scanner, word1, word2 and word3
    Each one is used in an assignment statement (has =) that assigns a value to the variable.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Super basic questions

    Quote Originally Posted by Norm View Post
    What is hello? A String variable or is it supposed to be a String? If so it should be enclosed in "s: "hello"
    Also statements should end with a ;
    Okay, I think I don't understand the difference between a String variable and a String, maybe?

    A String is a series of characters? The tutorial doesn't (and neither does my quick googling) talk about String variables.


    The above statement defines/declares a variable named word1 of type String
    If it is a variable named word1, it has a value as well as a name, right? Where is that/how do I define that?

    There are 4 variables defined in the original code: scanner, word1, word2 and word3
    Each one is used in an assignment statement (has =) that assigns a value to the variable.
    Right. But what/where is the definition of each, if they are defined? Shouldn't it be whatever comes after the "=" sign is the definition? But the only thing that comes after each of them is "scanner.next();" which I guess I'm not understanding what that is. I thought it was something telling the computer to scan the next bit of input - but then there is no input to scan - which is why I'm confused. Does that make any sense? Probably not...

    --- Update ---

    import java.util.Scanner; 
     
    public class Main {
     
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);  
     
            String word1 = scanner.next(); // "This"
            String line1 = scanner.nextLine(); // " is a simple" 
            String word2 = scanner.next(); // "multiline"
            String word3 = scanner.next(); // "input,"
            String line2 = scanner.nextLine(); // "" 
     
        }
    }

    ^ And this is the example in the tutorial - that I'm using to try and work out how this works - it itself generates an error, I'm very lost.
    Can anyone tell me - in the simplest terms possible - why it is wrong? If the example they give doesn't make sense, I feel slightly less bad about not being able to make sense of it..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Super basic questions

    itself generates an error,
    Please copy the full text of the error message and paste it here. It has important info about the error.

    If it is a variable named word1, it has a value as well as a name, right? Where is that/how do I define that?
    A value is given to a variable using an assignment stateement (has the =)
       variable = value;   // assign value to variable

    thing that comes after each of them is "scanner.next();"
    The next() method returns a String that is the value assigned to the variable.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Super basic questions

    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String str1 = "Java";
            String str2 = "Programming";
            String str3 = "Language";
     
            str1 = scanner.next();
            str2 = scanner.next();
            str3 = scanner.next();
     
            System.out.println(str3);
            System.out.println(str2);
            System.out.println(str1);
     
     
            ]
     
        ]

    Here's another example to perhaps better explain my confusion/ask my main question.

    Wouldn't the output be exactly the same without the middle set of text, the one with all the "scanner.next()" in? It looks to me like it would. So what is the scanner.next() actually doing?


    ----------

    and thank you, I do appreciate the response, even if I don't understand the content, maybe I just need a real life helper

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Super basic questions

    what is the scanner.next() actually doing?
    Have you read the API documentation for that method? It explains what it does.
    Go to this link: http://docs.oracle.com/javase/8/docs/api/index.html
    Find the class Scanner in the frame in the lower left.
    Click on Scanner will display the API doc for the class in the main frame.
    Scroll down in the documentation until you find the documentation for the next() method.

    Wouldn't the output be exactly the same without the middle set of text
    No. The variables are assigned a value when then they are declared:
    String str1 = "Java"; // assign an initial value
    Then the value of the variable is changed by an assignment statement:
    str1 = scanner.next(); // change value in str1 to the value returned by the next method
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginner Developer with Basic Computer Requirement Questions
    By akrohn in forum Java Theory & Questions
    Replies: 4
    Last Post: April 2nd, 2020, 03:44 AM
  2. Just a few basic questions
    By tloz in forum Java Theory & Questions
    Replies: 4
    Last Post: March 22nd, 2014, 08:42 AM
  3. basic java questions
    By Prajwal Kumar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 3rd, 2014, 04:48 AM
  4. Few very basic Java questions.
    By 01001010 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 13th, 2010, 01:14 PM
  5. Some basic questions.
    By trips in forum Java Theory & Questions
    Replies: 5
    Last Post: July 21st, 2009, 02:15 AM