• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

having problems with scanner variables

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. so i have a question, i am quite new (2 weeks ) to java, i have learned quite a bit about the language though. and am atempting to make a text based adventure., but theres a problem. i imported the scanner, made a scanner variable, used it to find out the persons name, then tried to say " so (name of the person) blah blah blah," but in the second part, it doesnt print the name of the person, it prints, i think the code for what a scanner is, or something like that. here is my code:


import java.util.Scanner;
public class therest {
public void start(){
Scanner name = new Scanner(System.in);
System.out.println("enter your name");
System.out.println("hello " + name.nextLine());
System.out.println("so " + name + "this is my first game" );
System.out.println("it is a text based adventure called ,the thief,");
System.out.println("it will be about using intoition to find as many er, um stealable,");
System.out.println(" objects as you can, and. well. stealng them");
System.out.println(" i hope you have fun, gooooood lucccccckkkkk!!! ");
}
}



and here is what the command line outputs:


enter your name
levi
hello levi(my name)
so java.util.Scanner[delimiters=\p{javaWhitespace}+][position=6][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]this is my first game
it is a text based adventure called ,the thief,
it will be about using intoition to find as many er, um stealable,
objects as you can, and. well. stealng them
i hope you have fun, gooooood lucccccckkkkk!!!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Levi,

Check the below code, I suppose this will work.
 
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You need to give your variables good and descriptive names (as well as CapitalLettersInClassNames). Have a look at these conventions. See if you can find the misprint where they have exchanged the words variables and constants. If you had called the Scanner object scan or input, rather than name, you would not have tried to print it out. Because you called it name, you got confused about its meaning, and that long line is what you get when you try to print the Scanner object.
I think there is a much better way to get the String than you have been told, based on the rule of thumb that you should declare local variables as late as possible; you do not need the name variable before you fill it with the input from the Scanner. That also avoids the dangerous practice of tolerating variables pointing to null.You can get an output similar to what you had like this: System.out.println(input);
 
Campbell Ritchie
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Choudhary wrote:Hi Levi,

Check the below code, I suppose this will work.

I don’t like that line, because
  • 1: You can do it more neatly with System.out.format(...) or System.out.printf(...)
  • 2: You are calling a static method on the name of an instance
  • Use spaces, not tabs for indenting.
     
    Campbell Ritchie
    Marshal
    Posts: 76872
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    levi sleight wrote: . . . it prints, i think the code for what a scanner is, or something like that. . . .

    You are not doing badly if you can work that out after two weeks Look up the toString method in the Scanner class, which should explain it.
     
    levi sleight
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    oh.lol i am so dumb. i tried that exact thing earlyer. and. um. well. i didnt realize i had everything right but the direction, i was doing Scanner = String. which was just making it null. ok thanks. also i checke out toString..... OOOOOOOOOOOOHHHHHHHHH , i had thought that a scanner was like a string, it could just, yknow scan, but its a totally different thing. thanks
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    levi sleight wrote:i had thought that a scanner was like a string, it could just, yknow scan, but its a totally different thing. thanks


    And you're not the first, which is why (as my colleagues will confirm), I don't like it.

    Winston
     
    Ravi Choudhary
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Campbell Ritchie
    Post Today 1:57:48 PM Subject: having problems with scanner variables
    Welcome to the Ranch

    You need to give your variables good and descriptive names (as well as CapitalLettersInClassNames). Have a look at these conventions. See if you can find the misprint where they have exchanged the words variables and constants. If you had called the Scanner object scan or input, rather than name, you would not have tried to print it out. Because you called it name, you got confused about its meaning, and that long line is what you get when you try to print the Scanner object.
    I think there is a much better way to get the String than you have been told, based on the rule of thumb that you should declare local variables as late as possible; you do not need the name variable before you fill it with the input from the Scanner. That also avoids the dangerous practice of tolerating variables pointing to null.




    You can get an output similar to what you had like this: System.out.println(input);





    @Ritchie

    Thanks Ritchie for your valuable suggestions, I will keep in mind for future reference.

     
    Campbell Ritchie
    Marshal
    Posts: 76872
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You’re welcome
     
    Enjoy the full beauty of the english language. Embedded in this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic