• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please explain why I get "cannot find symbol" error in Stdin and Stdout Statement in the code below

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Because compiler can't find the declaration of reference variables Stdin and StdOut on which you are invoking readString() and printf() methods. What are the type of these variables, you haven't declared them anywhere in your program. Please always quote from where you got this click here ---> QuoteYourSources
 
Sudeep Choudhari
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a java challenge from https://www.hackerrank.com/challenges/java-end-of-file

Sudeep Choudhari wrote:

 
Marshal
Posts: 79061
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

Please always indent your code correctly; look at lines 7 and 15 which are indented completely differently. The indentation is there so you can read your own code, and you will confuse yourself if you don't indent it properly.
Don't import all those packages; you are only using java.util.Scanner.
And look at line 12 where you have two declarations on the same line. Those are all features you can use to confuse yourself.
Look at line 19. There must be a simpler way to put that inequality. It is not at all obvious what it means.
Where did you find StdIn and StdOut? They are not terms used in Java® programming, but common in C/C++ programming.

If you ever get the code to compile you will have a nasty surprise with the loop in line 14. Since System.in is always open, that loop will run for ever ... or at least until an exception stops it.
 
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you ever get the code to compile you will have a nasty surprise with the loop in line 14. Since System.in is always open, that loop will run for ever ... or at least until an exception stops it.


It will die on line 16 because str has not been initialized.
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello sudeep,
actually i have just gone through your code,their i found some bad practice listed below:
1.Importing the unnecessary package
their are packages in your code (which are imported) but not been used.actually it just confuse one why have you make a import of such package,so make it your habit not to do so.

2.Importing the whole package
try not to import the whole package like import "java package.*" but instead import just the necessary class of the package.it doesnot have to do anything with performance or any overhead cost.actually keeping the things straightforward,clean and as of less ambiguity as possible is a good practice.with an exception,you have to type more.

try to write your code indently
this part is allready been covered by "campbell".

Now come to your problem:
Arrays are object too,so they need to be instantiated before use.

Hope it helps!
kind regards,
praveen
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Campbell Ritchie wrote:If you ever get the code to compile you will have a nasty surprise with the loop in line 14. Since System.in is always open, that loop will run for ever ... or at least until an exception stops it.


It will die on line 16 because str has not been initialized.

If we consider str has not been initialized then It will not even compile as It is a local variable which must be initialized before use.
 
Greenhorn
Posts: 22
2
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The Princeton online textbook on java programming uses StdIn and StdOut classes. Those were made for the sake of making some tasks easier to understand: http://introcs.cs.princeton.edu/java/stdlib/
I assume you have taken these statements from this textbook thinking it was standard java, but it is not. You can't make those work for the Hackerrank practice challenges, as you can't add extra classes as far as I'm aware. It's the reason for the "cannot find symbol" error, the compiler complains because it can't find those classes.
 
Campbell Ritchie
Marshal
Posts: 79061
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

That is useful information; maybe the hackerrank site doesn't make it clear where those classes come from.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The HackerRank shell code looks like this:

Beginning exercises teach you how to read data in and print it out.
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nah! here they do mention about what these stdin and stdout is about but I think use of such abbreviations create confusion --->Java Stdin and Stdout They closed Scanner I find that Starnge practice.
 
Campbell Ritchie
Marshal
Posts: 79061
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code snippet doesn't use stdin or stdout, nor StdIn and StdOut. It uses STDIN and STDOUT. I hope they take that to mean everybody knows what those C style abbreviations mean. And they have managed to mislead at least one user. It does however say System.in earlier.

I would suggest that trying to read from System.in until you read end‑of‑file is poor programming, since you would have to close System.in.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aren't these challenges just rewritten ones from their C ones?
That would explain the use of STDIN and STDOUT which are not commonly found terms in Java.
 
reply
    Bookmark Topic Watch Topic
  • New Topic