• Post Reply Bookmark Topic Watch Topic
  • New Topic

null field test in data inserted with Scanner.nextLine() failure  RSS feed

 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm trying to use in the constructor inserts from keyboard and then check whether any of the constructors fields remained empty. Let say my constructor is:



Thus I prepared method: insertConstruct() as follows:



and during creating the new object in some main method I call:



The problem is not with inserting data but with leaving the field empty. When I pass any field just with keyboards Enter, the empty test doesnt return me any message about it.
While it works if I build object with the constructor like:




What might case this failure?


cheers
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Przemek,

What is your best guess? What is difference between keyboard enter and null?

Can you please print inputs, check for their nullness and print their size etc in SomeClass constructor? It might give you some idea about what is happening.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you correct, you're saying, that you cannot get null pointer exception by adding an "empty" value from keyboard straight to constructor.
If my guess is correct, when you press return, you think you get an empty string, in fact it's not true, you get "\n" carriage return.

Someone will correct me if I wrong.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haeve you printed the input from nextLine?
What do you think nextLine reads? Most people have not been taught that bit correctly. But I do not think it is possible to get null from nextLine.
I suggest you throw a NullPointerException from that constructor (though many people would throw illegal argument exceptions). Because it is unchecked, don't declare it with a throws clause. There is another way to get that check:The Objects class was introduced in Java7.
 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried requireNotNull method as foolows:



but compiler returns me the error message:

X:\PG\study\temp>javac NotNullTest.java
.\NotNullClass.java:7: error: cannot find symbol
testWord = Objects.requireNotNull(Word, "null message");
^(Objects is pointed)
symbol: method requireNotNull(String,String)
location: class Objects
1 error


Anyway, somebody advised me to use compareTo() method (Comparable interface) and also I found the word.Length() to be the right way to check it. But requireNotNull seems to be the simplest one. It would if it work ;)
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What version of Java are you using?

If it's 8, then are you sure it's not pointing at the method name (it's requireNonNull, with an 'n', not a 't')?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies for the misspelling. It was a simple mistake. Thank you DT for noticing. Sorry about that.

Please ask for more details about what compareTo() is supposed to do. I cannot see that method helping you in this situation. The length() method of the String class might help, so would String#isEmpty.

Apart from my mistake, there appear to be two kinds of confusion here.
  • 1: About what Scanner#nextLine() actually does. Please tell us what your book says it does, as I asked earlier.
  • 2: Nomenclature. People talk about the null String but there is no such thing.
  • You can have an empty String; Fred Rosenberger says that is like having an eggbox with no eggs in. You can have a String reference which points to null, but that is different. It means you do not have an object behind that reference at all. Fred says that is like not having an eggbox in the first place. Don't say, “null String,” but, “empty String.”
    As I said last week, I don't think it is possible for Scanner#nextLine (see link provided previously) to return null, but it can return an empty String. I believe it is also not possible to get a String containing the line‑feed character (=\n) from a Scanner unless you change its delimiter. You can read about delimiters in the Scanner documentation and about nextLine in my old posts: try this one.
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think it is a good idea for you to use a static method for reading from the keyboard like that. It would be better if you had that method in a utility class. Also don't create a Scanner object every time you call that method. Create one Scanner as a private static field in your utility class and never close it. Remember that method is not thread‑safe.
     
    Przemek Geminski
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm using Java 7.0_75.

    When printing content of scanner (the empty insert - just Enter key) then nothing apear - blank line. But I gave up with checking it bu null test. What you prooposed (requireNotNull()) is sufficient for my current needs. Only that I can't turn it on.

    I can't write you more about compareTo() in this case because I'm not familiar with interfaces issue yet. I will investigate it and then I response.
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A blank line does not mean null. Find what happens when you pass null to System.out.
    System.out.println(null);
    System class documentation.
    The println() method documentation says it uses String#valueOf, so that last link tells you what happens if you pass null.
    To find out what you are getting from your Scanner, let's call it String line
    System.out.printf("\u201c%s\u201d%n", line);
    That will print posh quote marks (201c/201d = “”) with your input between them. You may be getting an empty String in which case you see “” or spaces so you see “  ”
    If you don't print quotes or similar you can't see the spaces and will think you have a blank line.

    If you are getting spaces or empty Strings, are you using nextLine?
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have already been told I made a mistake and requireNotNull is spelt wrongly.
     
    Dave Tolls
    Ranch Foreman
    Posts: 3068
    37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Przemek Geminski wrote:I'm using Java 7.0_75.

    That's OK (my mistake, thought Objects was a Java 8 thing, but it turned up in 7).
    Fix the typo/spelling on the method and you should be good to go.
     
    Przemek Geminski
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    right!
    it was so tiny typo that I miss it as well....
    I fixed and it is ok.
    thanks!
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    Przemek Geminski
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Please ask for more details about what compareTo() is supposed to do. I cannot see that method helping you in this situation. The length() method of the String class might help, so would String#isEmpty.


    Ok Campbell Ritchie, heres how compareTo() can help in validating nulls from scanner:
    1.

    2.


    3. In test class



    cheers

     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You started the quote tags and didn't finish them. Don't worry; I have sorted it out.

    Good grief! You do not have any nulls in that code; you have the empty String but that is different. Only use compareTo and == 0 for equalities if the documentation of the class tells you so to do. I have only ever seen that for BigDecimal (here too). For Strings you should use the isEmpty() method, which I mentioned earlier. Your compareTo method never returns a negative number. You cannot therefore show that
    signum(a.compareTo(b)) == -signum(b.compareTo(s))
    and you are therefore not implementing Comparable correctly.
    You will of course fail to get 0 ever because of another mistake in the compareTo method.

    I am afraid you have managed to show the compareTo method not helping at all, but hindering your comparison of your input from your Scanner. As I said earlier, you cannot get nulls from a Scanner. You either get a real String or an Exception.
     
    Przemek Geminski
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    :/
    hm, pehraps I wrongly expressed what I meant. The goal is to prevent scanner from inserting empty string. I know there is many better ways of handling this issue but our task (on my course) is to do it by implementing Runnable. Does it make any sense?
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No.

    Are you using Scanner#nextLine?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!