Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!

Md Zuanyeed Kamal

Ranch Hand
+ Follow
since Dec 02, 2018
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
16
Given in last 30 days
5
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Md Zuanyeed Kamal

Please explain what you are getting confused about. Also, you didn't answer my question about whether Schildt wrote that code.



The I/O file system  seems confusing as still I didn't understand the concepts reading the chapter . Like why do we use FileInputSrteam, InputStream, inputbufferedreader and when would I have to use reader. Those stuffs are not clearly explained by Herbert Shcild in the book.
Yes, the code has been copied directly from the herbert Schild Complete reference 11th edition chapter 13, which I already acknowledged in my very first post.  
Thanks
2 weeks ago

Campbell Ritchie wrote:Did Schildt really write that sort of code? It is full of errors.
ShowFile is not a good name for a class, but a method.
Don't use anything called XYZInputStream to read a text file. Use something called XYZReader.
Don't close anything explicitly. Use try with resources instead.
Don't use the read() method, an abomination really awkward to use. Wrap your reader so as to buffer it and read line by line.
The loop isn't quite correct; you are not stopping when you get to the EOF character but after that. That is why you are reapeating (i != −1) in lines 39‑40. If you want to stop on the −1, you would need something like this:-You need to assign to i before the test; since > has a higher precedence than =, you have to wrap the left half of the expression in an additional pair of () so the = is executed first.
That code has far too long a main() method.
This is what I would suggest, using the NIO classes to open a Path:-



This chapter is really too confusing or me, and  while reading  the chapter from the book, I got more confused!!!
2 weeks ago

Ricky Bee wrote:

Md Zuanyeed Kamal wrote: This code has been taken from java 11th edition by harbert schild chapter 13 .



Hello.

I'm reading from that same book - a couple of chapters behind you.

I see you are using Eclipse to practice the examples provided. That will only get you into trouble (like the very situation you are describing in this topic).

You can of course replicate the examples in any IDE, but the author makes an early point in the book by stating that a simple text editor plus the command line should be utilized.

IDE's have a specific way to structure projects and files inside folders (with packages in the mix) - Ron's response above makes it clear that there are issues in your code related to that - you are not controling where files are being stored.



I got your point. Eventhough the .java file  is in the eclipse directory,  I compiled the program from command line. Yeah you are right, notepad might be the best way to run the code from this chapter as this chapter is too much confusing for me.
Thanks for your suggestion, I appreciate it.
2 weeks ago

Ron McLeod wrote:

Md Zuanyeed Kamal wrote:C:\Users\zunayeed\eclise-workspace\Java_Problems\src\inputOutputWithTryCatch>java ShowFile TEST.TXT
Error: Could not find or load main class ShowFile
Caused by: java.lang.ClassNotFOundException: ShowFile


1. It looks like you are trying to run the application from a source directory.  How did you compile the ShowFile.java file?

Is your class located in C:\Users\zunayeed\eclise-workspace\Java_Problems\src\inputOutputWithTryCatch, or in another directory like C:\Users\zunayeed\eclise-workspace\Java_Problems\bin\inputOutputWithTryCatch?

2. Since your class is in the inputOutputWithTryCatch package, you should be trying to run it using the fully qualified class name: inputOutputWithTryCatch.ShowFile

3. Try running your application from the top of the directory structure for the class files, not inside the inputOutputWithTryCatch directory:
    C:\Users\zunayeed\eclise-workspace\Java_Problems\bin>java inputOutputWithTryCatch.ShowFile TEXT.TXT



I tried in numerous ways, but still I am unable to read the files, even though I have been able to create the class files using command javac ShowFile.java in command line. I have provided the screenshot of the code in command line.
Thanks
2 weeks ago

Paul Clapham wrote:Just a suggestion: if you're going to print something and give up when an exception is thrown, instead of a one-line description, print something more informative. Like the actual description of the exception and the stack trace (which tells you where it happened). Like so:




Actually, I am confused about the information of the code.
2 weeks ago

Ron McLeod wrote:

Md Zuanyeed Kamal wrote:I tried >java ShowFile TEST.TXT  command in command line, but I got an error.


What was the error?


I forgot to mention the information below
Purpose of the program: This  program uses read( ) to input and display the contents of a
file that contains ASCII text. The name of the file is specified as a commandline
argument.

Here my another doubt is where should the file be located ?
Overall, I didn't understand the code .

Some information author added:   In the program, notice the try/catch blocks that handle the I/O errors that
might occur. Each I/O operation is monitored for exceptions, and if an
exception occurs, it is handled. Be aware that in simple programs or example
code, it is common to see I/O exceptions simply thrown out of main( ), as was
done in the earlier console I/O examples. Also, in some real-world code, it can
be helpful to let an exception propagate to a calling routine to let the caller
know that an I/O operation failed. However, most of the file I/O examples in
this book handle all I/O exceptions explicitly, as shown, for the sake of
illustration.


2 weeks ago
I am not sure how to run the  code. I created a  text file in the package, but I am not sure how that text file is linked to the code. This code has been taken from java 11th edition by harbert schild chapter 13 .

I tried >java ShowFile TEST.TXT  command in command line, but I got an error.



In the code below, how the TEST.txt file is related to the program, I am not getting it .

Thnaks in Advance
2 weeks ago

Stephan van Hulst wrote:Uhh, how else would it know of what service provider interface to load implementations?



Sorry i didn't get your question. Can you please clarify?
Thanks
3 weeks ago

Service providers are supported by the ServiceLoader class. ServiceLoader
is a generic class packaged in java.util. It is declared like this:
class ServiceLoader<S>
Here, S specifies the service type. Service providers are loaded by the load( )
method. It has several forms; the one we will use is shown here:



My question is: In the load method argument, why do they provide Class ? It seems to me unclear.
Source: Java the complete reference 11th edition page 690  by herbert Schildt

Thanks
3 weeks ago

Carey Brown wrote:We need to see what JAVA_HOME and PATH are set to.

Type:

path
set JAVA_HOME


paste results back here.



Now I got it right. Now it is working fine. Thank you so much.
4 weeks ago
Problem is resolved. at last, I got it right. I removed rest of the path variables related to java, and the problem is fixed.  
4 weeks ago
There were two types of environmental variables: user variables and system variables. wherein would I have to set the path  ?
4 weeks ago

Carey Brown wrote:Try:

cd "c:\program files\java\jdk-12\bin"
java.exe -version


If this doesn't work you've got a bad install. If it does work then something is preventing you from accessing it from a command window. Usually PATH would be the culpret, but may not be in your case.


I tried the command you said. That one worked. So, I have originally JDK 12 installed. But, how can I fix up the path issue ?
4 weeks ago

Stephan van Hulst wrote:You need to tell Eclipse where your JDK is located. Set the JAVA_HOME environment variable to the JDK root folder.


I did that. It didn't work. However, I installed eclipse 4.11.1 . Now it is working. Eclipse has its own compiler and JRE. That is why, it is working fine, I guess. But when I comes to command line, I am still getting error when I tried to check java -version. Still I couldn't get any response that I have jdk 12 installed.
Expert suggestion please.
4 weeks ago
Do I need any JRE ? I only have JDK 12 installed in my laptop.
4 weeks ago