File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes having problems with scanner variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "having problems with scanner variables" Watch "having problems with scanner variables" New topic
Author

having problems with scanner variables

levi sleight
Greenhorn

Joined: Sep 19, 2012
Posts: 19
. 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!!!
Ravi Choudhary
Greenhorn

Joined: Sep 03, 2009
Posts: 16
Hi Levi,

Check the below code, I suppose this will work.


RaviKumar
SCJP 5.0, Preparing for Next
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38485
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 38485
    
  23
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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38485
        
      23
    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

    Joined: Sep 19, 2012
    Posts: 19
    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
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7703
        
      20

    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


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Ravi Choudhary
    Greenhorn

    Joined: Sep 03, 2009
    Posts: 16

    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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38485
        
      23
    You’re welcome
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: having problems with scanner variables