Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Cannot validate input stream before attributing it to variable

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

       
       


        if (cont == true)
        {

            System.out.print("Enter the first number: ");
            int n1 = Integer.parseInt(input.next());
            System.out.print("\nEnter the second number: ");
            //int n2;
            //if (input.hasNextInt()) { n2=input.nextInt(); }

            int n2 = input.nextInt();

            System.out.println("\nResult: "+Integer.toString(math(op,n1,n2)));
        }
    }
}
The above snippet of my code has a fairly straightforward set of two commented lines that are supposed to replace the line  right below it. It simply add some validation to the input in the input stream. But whenever I try to compile the code it shows the following error message:

error: variable n2 might not have been initialized
  System.out.println("\nResult: "+Integer.toString(math(op,n1,n2)));
                                                              ^

as if the variable n2 was out of scope but the variable was declared outside the if block. Anyhow, if instead of simply declaring the variable "int n2;" I declare and initialize it -- such as "int n2=0;" -- with any value the compiler time error would disappear and during execution whatever input I would enter  for the value n2 would be stored in n2 and the program would perform accordingly. It is not clear why that is happening. Any insight would be appreciated.  
 
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never write == true and == false, which are both error‑prone and poor style.
Never if (b == true) ...
Always if (b) ...
Never if (b == false) ...
Always if (!b) ...
Because you are initialising that variable inside the if, the compiler regards it possible that your boolean is false, in which case you are using that variable without it having been given a real value. That isn't permissible.
Don't mix Scanners and Integer#parseInt. Use Scanner#nextInt(). It is better to have a utility class for suh input. You will find part of it here. This is a suggestion for the simplest form of the nextInt() method:-Use that. No need to mess around with parseInt, or if *input.hasNextInt()) ... or anything. You can use the utility class for ever as long as you acknowledge its origins.

[edit] if *input.hasNextInt()) ... should have read if (input.hasNextInt()) ... hextInt() would read better as nextInt()
 
Marshal
Posts: 6670
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please UseCodeTags (that's a link) when posting code.
 
Lazarus Wald
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Also, please UseCodeTags (that's a link) when posting code.



Sorry, next time I will insert the snippet of code within tags
 
Lazarus Wald
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Also, please UseCodeTags (that's a link) when posting code.



I know I read about the “Never if (b == true)” somewhere but old habits die hard. I will void it at all cost now.  It seems that the compiler is preempting the possibility that the value of the boolean condition is false, and thus the possibility that the variable inside the if block is never initialized.

I used the method you sent me  in order to handle the user numeric input. It is a very basic and simple program, but I was confused about the compiler behavior and why once the variable was not only declared but also initialized  outside the if block it simply worked out fine. But now it is clear.

I used the following sub-routine (method you sent me with a little modification):



Thanks for the insights!
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider moving the declaration of that variable; if you can restrict its scope to inside the if, it will always have a valid initialisation. That is, if you able to move it.
I can see a drawback to your method: you are creating a new Scanner object every time you want an int. You only need one Scanner reading System.in per application.
 
Lazarus Wald
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Consider moving the declaration of that variable; if you can restrict its scope to inside the if, it will always have a valid initialisation. That is, if you able to move it.
I can see a drawback to your method: you are creating a new Scanner object every time you want an int. You only need one Scanner reading System.in per application.



Roger that!

So, I changed the code so that a scanner object is only instantiated once in the application. I used the scanner object from the main function by passing it as a parameter to the value function, as follows:

 
Knute Snortum
Marshal
Posts: 6670
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above is perfectly fine, but you can also declare the Scanner object outside of any method.  Then you don't have to pass it in.
Just a note that it is not always a good idea to get variables into methods like this, but in this case, there should only be one Scanner per class, so it works.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did intend the method to be added to the incomplete utility class I posted earlier this week, which has a Scanner constant already. Just the same as Kunte's Scanner field. As Knute says, you need one such Scanner full stop. It is inefficient to create a new Scanner every time you want new input, but keyboard input is so slow, you won't notice that inefficiency.
Only use \n and \r if you have been asked for those characters specifically. I intended the whole message to be printed, and that would include “Please enter...”, and wouldn't require an additional empty line. I don't usualy start output with a new line; if there is previous output to the terminal, it is the responsibility of whichever code wrote that output to move you onto a new line.
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!