• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the difference between import java.util.* and import java.util.Scanner;  RSS feed

 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really don't understand why my code won't compile when I import the Scanner class throut this statment : -
thought the   Scanner class is inside the util package !!

I am trying to read double numbers from a file named data.txt , then to sum them up and print the result to the standard output;
the below code won't compile and dosn't seem to import the Scanner class



but this one below works and prints the result

 
Tim Moores
Saloon Keeper
Posts: 4035
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the below code won't compile

What does this mean? What exactly happens when you try to compile?

Both codes compile fine for me. While there is a difference between the two import statements (namely in how other classes in the java.util package are treated), it doesn't make a difference to this particular code (because it uses no other classes from java.util).
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Tim mores for your replay.
I have found the reason . there is a class inside my package chapter11 also named Scanner and I didn't know about it. and it looks like the compiler have imported  it first becouse it is much closer to the main .
sometimes such  errors takes a bit time to figure out becouse the code looks very fine and without errors.

below is the error that I recieved in the standard output

Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at chapter11.Scanner.<init>(Scanner.java:46)
at chapter11.Test.main(Test.java:13)
C:\Users\Sami Kassoum\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 9 seconds)
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you had included the error messages you were getting, we could have told you what the problem was right away. The error message is pretty clear which class the error was occurring in: "chapter11.Scanner"

This is why you should always try to TellTheDetails (← click that link) when asking questions on the Ranch.
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the next time I would include the error message for sure
thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!