top of page

Interesting Notes about Java Scanner

Perusing through a considerable measure of Java discourse, it appears like there has been a misguided judgment with respect to the utilization of Java Scanner. It comes into my consideration that Scanner class is just for newbies and the utilization of Reader class is for development programming. I ask to oppose this idea. The Scanner class in java has been conceptualized to make our life less demanding and all the more significantly this class likewise epitomizes a great deal of intense strategies that is more progress and less demanding to use than those techniques accessible in BufferedReader. In view of convenience this class is putting forth, this opens up a great deal of talk in a few java discussions. Yet, I am telling as early as now, in the event that you have not yet took in the utilization of Java Scanner, begin learning it at the earliest opportunity in light of the fact that soon its execution will definitely supplanted the class BufferedReader.

In the interim, I might want to examine on the utilization of Java Scanner by refering to one illustration i got from one of the java gathering. It goes this way

While running underneath code in perusing the client contribution from console, I am confronting an issue. Why it neglected to peruse the info appropriately?

import java.util.Random; import java.util.Scanner; import javax.swing.JFrame; import javax.swing.ImageIcon; import javax.swing.JLabel; import java.awt.Container; public class test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String userInput; System.out.println("Type yes"); userInput = scan.next(); if (userInput == "yes"){ System.out.println("Your input is correct"); } }

You wouldn't trust it however eventhough the issue is basic, the dialog about the subject in reddit is incredibly complex. Possibly in light of the fact that reddit is an open discussion and the balance are not that great that is the reason there are a ton of selfish wannabe java novice where in truth the issue is exceptionally basic. It's excessively interesting how it goes in reddit. In spite of the fact that the dialog are not beneficial, the inquiries are absolutely great and it should be appropriately subsequently I might want to talk about and fathom it here legitimately.

While the source code exhibited appears to be very okay there are a few defects on it. Let me first bring up the over the top utilization of imports. Those are superfluous, expel every last bit of it aside from the line import java.util.Scanner; The other import proclamations won't create a blunder anyway it's a decent practice to diminish pointless import to minimize the memory impression of the system.

Second issue is the utilization of this statement userInput = scanner.next(); right away, this code is alright nonetheless in the event that you are an ordeal java software engineer, you would know the issue.

There is a know conduct of java scanner that in the event that you read from console the newline toward the end of the info is not getting devoured. This conduct is clear on the off chance that you require numerous contribution from console. I would truly propose to utilize nextLine (Check this out). At any rate similarly as the source code above is worry, there would not be an issue here in light of the fact that it is requesting single information. This issue on the utilization of Scanner is called attention to in http://javatutorialhq.com/java/util/scanner-class-tutorial/

The primary issue on source code introduced is the code charge == "yes". This is the fundamental issue here. Keep in mind that when you look at two articles which for this situation a String, the utilization of equivalents is empowered. The = administrator is utilized to think about two primitive information sort. String is not a primitive information sort, it is an Object. When in doubt, use equivalents to look at two Objects.

The last issue is that the scanner item is not close along these lines it might result to memory overflow. Simply include scan.close() toward the end of the code and the source code is finished.

Here is the right java code that would read a contribution from the console and do as such preparing a while later.

import java.util.Scanner;

public class test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String userInput; System.out.println("Type yes"); userInput = scan.nextLine(); if (userInput.equals("yes")){ System.out.println("Your input is correct"); } scan.close(); }


  • Black Facebook Icon
  • Black Twitter Icon
  • Black Pinterest Icon
  • Black Instagram Icon
FOLLOW ME
SEARCH BY TAGS
No tags yet.
FEATURED POSTS
Check back soon
Once posts are published, you’ll see them here.
INSTAGRAM
ARCHIVE
bottom of page