Results 1 to 14 of 14
  1. #1
    gcampton Guest

    Default can I use scanner as class field?

    "can I use Scanner as class field?" I used this line for google and found some results, the reason I ask is someone told me I shouldn't but being as persistent as I am I didn't take it for granted.

    COMP101 - CONSTRAINED AND UNCONSTRAINED ARRAYS

    was one example of using Scanner as field.

    Java Code:
    import java.util.*;
    
    public class appleTest
    {
       private Scanner dummy = new Scanner(System.in);
       private Scanner input = new Scanner(System.in);
       private boolean loggedIn=false;
    //   etc....
    
       public appleDriver()
       {
          String junk;
          System.out.print("Enter something: ");
          junk = dummy.nextLine();
       }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If it is a property of the class then yes. If it's used in only one method then declare and use it in that method.

    P.S As used in your code it's called an instance variable. Class field can cause problems because class variables are static variables.

    P.P.S. Your code won't compile like that.
    3P.S : Don't keep postponing reading Sun's tutorial.

  3. #3
    sridevisala is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    i want an example of using hibernate with jsp,sybase,tomact4.1 version

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by sridevisala View Post
    i want an example of using hibernate with jsp,sybase,tomact4.1 version
    .. with onions and sauce? Please specify the topping you would want too ...

  5. #5
    gcampton Guest

    Default

    Quote Originally Posted by r035198x View Post
    If it is a property of the class then yes. If it's used in only one method then declare and use it in that method.

    P.S As used in your code it's called an instance variable. Class field can cause problems because class variables are static variables.
    my bad, so it can be static as well would that be beneficial?

    Quote Originally Posted by r035198x View Post
    P.P.S. Your code won't compile like that.
    yea i left out return type, but as you can plainly see it's junk code. Not particularly in the habit of checking syntax or spelling when writing junk examples.


    Quote Originally Posted by r035198x View Post
    3P.S : Don't keep postponing reading Sun's tutorial.
    why do you think I don't read it? Do you seriously have every single thing in that tutorial locked away in your brain like an encyclopedia? while possible I doubt it... and being that I am learning 4 languages at once at UNI, programming techniques are one thing but learning each and every tiny detail in each language within a couple months would be close to impossible, sun tutorial, cplusplus.com, sh/bash shell scriptings guides are references I use regularly and have to use regularly if I want to pass... but some things are ambiguous like this. I checked Scanner class in API and googled as I have already mentioned.
    Last edited by gcampton; 11-09-2009 at 11:02 AM.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by gcampton View Post
    ..
    why do you think I don't read it? ...
    Because I remember at least two other threads of yours in which you were struggling with these (or very similar) basics and I pointed out that they were well explained in Sun's tutorial.



    Quote Originally Posted by gcampton View Post
    ... Do you seriously have every single thing in that tutorial locked away in your brain like an encyclopedia? while possible I doubt it...
    I have a feeling you would prefer if I don't reply to your threads so I won't do it anymore.

  7. #7
    gcampton Guest

    Default

    Quote Originally Posted by r035198x View Post
    Because I remember at least two other threads of yours in which you were struggling with these (or very similar) basics and I pointed out that they were well explained in Sun's tutorial.


    I have a feeling you would prefer if I don't reply to your threads so I won't do it anymore.
    That's not correct I do highly value your opinion as your one of the few on here that know what they're talking about, I just get tired of hearing you tell me to read it when I AM reading it :P

    I will try to limit dumb posts though.. I knew it was fine to use scanner in this way but I questioned myself because somebody not so informed told me I couldn't use it in this way.
    Last edited by gcampton; 11-09-2009 at 11:24 AM.

  8. #8
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    I've never encountered an instance where I need to use a Scanner as a field. Most of the time, as r035198x mentioned, they are used in a method to retrieve data from the keyboard or a file, and I have used them, on occasion, to parse data in a String (which they do a good job).

    If the question is "can you", then of course, yes. You can have any class, interface, or enum object as a field, since a field is just a variable.


    In the example given, they created a STATIC field, keyboardInput. They did so to make it easier to read from the keyboard. So, instead of needing to create a Scanner object in each method that has user input, they decided to instantiate one, static instance, and they use it throughout.

    Hopefully, that answered your question. If not, don't hesitate to ask for clarification. If you have a specific example of when you might want to use a Scanner, we can clarify whether it's a good idea.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  9. #9
    gcampton Guest

    Default

    thanks codes.

    r035198x, I'm 31 years old, last programming I did was goto commands on commodore 64(1987?), if that counts... since then I have done zero coding, except for learning <head><body> in html back in 1998 and in 2 months have taken in how to do basic coding in java, c++ up to file reading/writing arrays, polymorphism etc.
    bourne shell scripting: had to learn how to use all the unix commands/utilities, and then VI on top of that(it's part of the course for some funny reason).
    database design: access, winsql, and postgres, open office(base, calc, word).

    I am at the top of my class (97.5 out of 100 on my first java assignment), so while my questions may seem dumb like they can simply be looked up or would be known if I had just "RTFM!", usually it's not simply a failure to do so on my part, but me looking and not being able to remember where to look to find what I need or simply the texts don't cover it,
    or and mostly this option has been thus this far, I ask for further clarification because the texts either use incomplete examples(enums for instance)or use different data types to what I want to use.

    EDI: could have even got higher marks on that assignment if only i used OOPL principles a bit better :(
    Last edited by gcampton; 11-09-2009 at 02:43 PM.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Erm, like I said you don't have to worry about me anymore.

  11. #11
    gcampton Guest

    Default

    whatever mate, sorry that I offended you
    Last edited by gcampton; 11-09-2009 at 03:04 PM.

  12. #12
    gcampton Guest

    Default

    Fact is this is the beginner forums, so pretty much every single question on here is going to be a nub question and could just be answered with a "RTFM"(read the effin manual). So why does this forum exist exactly?

  13. #13
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Because some of us enjoy sharing our experience with others. Personally, I find tutoring one of the most challenging and rewarding roles as a programmer.

    I mean, this is a NEW TO JAVA forum. This is the place where "noob questions" should flourish. This should be a place where inexperienced programmers can learn from more experienced programmers. This should NOT be a place where experienced programmers can act "snobbish". I mean, we all were "noobs" at some point.
    Last edited by CodesAway; 11-10-2009 at 01:07 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  14. #14
    gcampton Guest

    Default

    hey funny they are an anagram... snobish, noobish ... almost

Similar Threads

  1. Using the scanner class
    By danielwestjr in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 10:49 AM
  2. Class Reflection: Finding field infomation
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:09 PM
  3. Getting class field information using Reflection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 03:20 PM
  4. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 07:01 AM
  5. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •