How to use the scanner

Discussion in 'Programming' started by Panerai, Jul 14, 2015.

Thread Status:
Not open for further replies.
  1. I sound like a noob lol.
    Title explains it all
    before i get hate, i would like to say i am new to this
     
  2. clip

    Benefactor

    • Winner Winner x 11
    • Funny Funny x 5
  3. I think he means the first one.
     
  4. Scanner scanner = new Scanner() creates new object

    ---

    Scanner scanner = new Scanner(System.in) allows input I guess

    sysout("What's your name?")
    String inputText = scanner.nextLine(); gets answer for the person's name.
     
  5. Damn thats some good information.
     
    • Like Like x 1
  6. He posted on here because he wants custom help from forum members

    To start off with using Java's Scanner class, you need to first create an instance of the Scanner class, because the methods aren't static. You can use any variable name really, but I'm going to use keyboard, as it makes sense in the program.

    Code (Text):
    Scanner keyboard = new Scanner(System.in);
    So in the (), we put System.in, because the Scanner we're using is going to be from keyboard input. If we wanted the Scanner to be a file, it'd look something like this (using "test" as the variable):

    Code (Text):
    Scanner test = new Scanner(new File("test.txt");
    In the (), instead of using System.in (keyboard input), we use new File (input from a file). So now I'll explain some common methods from Scanner that we can use in the program (note this'll be from keyboard input, not a file):

    keyboard.nextLine(); — this gets a String from keyboard input

    keyboard.next(); — an alias for nextLine()

    keyboard.nextInt(); — this gets an integer from keyboard input

    keyboard.nextBoolean(); — this gets boolean (true or false) from keyboard input

    keyboard.nextDouble(); — this gets a double (decimal value usually) from user input

    There are plenty more options too. If you use an IDE like Eclipse, it should give you all the options after you type keyboard. in your code.
    [​IMG]
    Above is just a few in Java's scanner class.

    Hope that helped ;)
     
  7. no it wouldnt
    scanner would be good, or keyboardScanner

    if i typed keyboard i'd expect a reference to some class named Keyboard

    other than that the rest of that info is correct
     
    • Funny Funny x 1
  8. Yet Barry Burd uses keyboard as a variable in the most popular Java for Beginners book...

    Using keyboard as a variable isn't incorrect in the first place
     
  9. Barry Burd isnt a very good programmer.
     
    • Funny Funny x 1
  10. Now I remember you. You're that guy who thinks he's a supreme Java dev, because you use the most complicated variable names.

    keyboardScanner though, really dude? keyboard explains enough. Btw, from looking at Java's packages, there's no java.lang.Keyboard class, nor java.io.Keyboard class, and when I try to instantiate the class Keyboard, it's telling me the class ain't found, smartass.

    Barry Burd is a very good Java programmer, which is why many people eager to learn Java bought his books. As for you, you probably either skidded some Java tutorial on Youtube or skimmed the basics of Java on Oracle for 2 minutes.
     
  11. lol..
    i dont think im some supreme java dev because i use complicated variable/field names. your objects name should reflect on what they are. if i have a class (Scanner) thats scanning the keyboard for input, i'd name it scanner, or better yet keyboardScanner.

    your entire second paragraph is flawed because you're making the implication i can't make my own class called Keyboard (LWJGL has Keyboard.class i believe?)

    barry burd is a mediocre java developer, if you want good java developers, go look at authors of java platform features. don't get angry at me because i'm not following whatever tutorial you followed when you were starting.
     
    • Funny Funny x 1
  12. Me getting mad? You just wrote an angry paragraph about why keyboard is a bad variable name xD.

    keyboard is a good variable to use because keyboard reflects what is being used as an input—System.in. What is System.in? Keyboard input. So why would I make an entire variable named keyboardScanner when I can just make an easier one? Oh right, you're probably one of those guys who writes String commandLabel in your plugins, lmfao.

    Barry Burd isn't one of the best Java developers out there, but he's a whole lot better than you
     
  13. i was calm the entire time but ok. im not the one calling anyone names.
    if you ever get a job in programming (i hope to god you don't) have fun trying to get away with giving undescriptive names to fields/variables/classes/methods.
    keyboard is a terrible variable name to use because it's not reflecting on it's object (which is Scanner, not some object named Keyboard or something similar). you should really take some time to stop looking at mediocre spigot plugins src and look at actual enterprise java source code.0
     
    • Funny Funny x 2
  14. Naming variables is up to developers. They really don't even need to follow convention if they really don't want to.
     
    • Agree Agree x 1
  15. This just made you look really stupid @Vibes

    God hope I don't? Actually, i was planning on it, although I'm about to turn 13 at the moment.

    You on the other hand look like a 15 year old who failed English class, and thinks he's amazing at Java for naming his variables the way Java supports. I really don't care what variable names I use. Keyboard is specific to the System.in in the instance. keyboardScanner is long and unnecessary, and scanner doesn't explain the specific input well to new Java beginners. This was just my example of a variable name for beginning programmers—I mostly use 1-3 letter variable names, because I can remember what the variables are, which are a big advantage since I won't have to type longer variable names.
     
  16. i cant tell if you're being sarcastic about turning 13 or not. and how did it make me look stupid? good developers will follow java practices and bad developers (e.g., you) won't follow java practices. i don't see how this is complicated.
     
    • Funny Funny x 1
  17. There's a difference between convention and opinion. The naming of his variable follows proper Java convention, so therefore it is named properly.

    In the end, developers choose to name their variables what they do because it doesn't matter what anyone else thinks, it matters what they think.
     
  18. sure, you can do whatever you want when you're developing a spigot plugin by yourself - no one really cares. try developing in a group of skilled developers sometime. giving variables/fields names that aren't intention revealing only contributes to sloppy code. choosing good names takes time but saves more than it takes (because keyboardScanner is too descriptive and too long for shieken).
     
    • Funny Funny x 1
Thread Status:
Not open for further replies.