• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to write import statements and use it's class components in one line  RSS feed

 
Ranajoy Saha
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all

I was wondering if I could write this code in one line.


Can I combine "import java.util.Scanner","Scanner sc = new Scanner(System.in);" and "char ch = sc.next().charAt(0);" in one statement? The object created from the class Scanner may be anonymous but it doesn't concern me! Any help will be appreciated!

Regards,
Ranajoy Saha
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import statements have to be at the top of the program code, and just after an optional package statement.

You could combine the next two statements like this:



But that's a bad idea, because you want to reuse the sc instead of recreating it each time.

If you post what you wanted to do with this combined line, or why you wanted to do it, someone may be able to help you with that.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if you can get the whole expression to return a char.
Try
System.out.println(new java.util.Scanner().nextLine().charAt(0));
new ...Scanner() retuns a Scanner reference
nextLine() returns a String reference.
charAt(0) returns a char and will throw an out of bounds exception if you push the enter key without writing something else first.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! I forgot to pass System.in as an argument to the Scanner constructor. As KS says, that looks like poor quality code.
 
Ranajoy Saha
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick replies from both of you! Actually I always write it separately to increase readability and I always avoid sloppy codding. The reason why I asked this is because some time my friends asks me to tell them the syntax of taking an input on facebook and as facebook doesn't quite format the code nicely it's becomes terrible for someone to comprehend it, that's why everything in a single line looks neat while you are chatting, so I asked. Thanks for all the help!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This compiles and runs

As you can see it is in one line. Of course it is not readable.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want to have the import statement at all, you can use the full name of the class, java.util.Scanner, instead of the short name Scanner.

But that will not make your code easier to read:

 
Ranajoy Saha
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranajoy Saha wrote:some time my friends asks me to tell them the syntax of taking an input on facebook and as facebook doesn't quite format the code nicely

Off topic: try using Shift-Enter to go to the next line and continue.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranajoy Saha wrote:Thanks for the quick replies . . . my friends asks me to tell them the syntax of taking an input on facebook . . .
You're welcome. you should instead direct them to somewhere with the facilities to format code legibly. Somewhere like here
 
Ranajoy Saha
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok got it! Campbell Ritchie
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!