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
3
Received in last 30 days
1
Total given
17
Given in last 30 days
1
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Md Zuanyeed Kamal

I am not getting how does this code work - especially the n3.p.p.id part - and tracking the objects seems difficult to me. Can anyone help me out please ?  thanks
I removed the new keyword in front  of reference  Fizz f3. Now when I run the program, I get true true . Here my question is z is referring to null eventhough it is a Fizz type variable . will it make f3 null as well ?
Revised version:

class Fizz {
int x = 5 ;
public static void main(String[] args) {
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1,f2); // removed new keyword
System.out.println((f1 == f3) + "  "+ (f1.x == f3.x));

}
static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z = x;
z.x = 6 ;
return z;
}
}


Can anyone help me with what will be the output and why ?


The error I got is :
zunayeed@Zunayeed-hp:~$ javac Fizz.java
Fizz.java:14: error: <identifier> expected
z.x = 6 ;
  ^
Fizz.java:15: error: illegal start of type
return z;
^
Fizz.java:15: error: <identifier> expected
return z;
       ^
3 errors

Then when I try the next command,  got false true as answer :
   zunayeed@Zunayeed-hp:~$ java Fizz
    falsetrue

 

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 months 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 months 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 months 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 months 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 months 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 months 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 months 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
2 months 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
2 months 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.
2 months 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.  
2 months ago