megha joshi

Ranch Hand
+ Follow
since Feb 20, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by megha joshi

Thanks..Any idea on how to do that..On command line, more usually expects enter key input from user. How to do the same in this code? Thanks again!
15 years ago
Thanks..I went through it though issues mentioned in the article, are not relevant for this case. Any other suggestions? Thanks again!
15 years ago
Hi all,

I want to use stream based IPC in Java. To test this I am trying to exec unix more filename command from my java code. Interesting thing is commands like ps, man, ls -l execute fine using the code below. But executing more on a big file , which requires enter input from keyboard occasionally fails. I would like to give input to this unix process from my java code..but I am not sure how to do that. The code I have so far is as follows:
15 years ago
Hi all,

I want to use stream based IPC in Java. To test this I am trying to exec unix more filename command from my java code. Interesting thing is commands like ps, man, ls -l execute fine using the code below. But executing more on a big file , which requires enter input from keyboard occasionally fails. I would like to give input to this unix process from my java code..but I am not sure how to do that. The code I have so far is as follows:

15 years ago
import java.util.Scanner;
public class GCD{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a;
int b;
int n;
System.out.println("GCD FINDER");
System.out.println("Enter integer \"a\": ");
a = in.nextInt();
System.out.println("Enter integer \"b\": ");
b = in.nextInt();
n = 1;
int f=1; //Local variables must always be initialized, else compiler error
while (n <= a) {
if (a % n == 0 && b % n == 0) {
// int f // You are redeclaring f inside loop why???
f = n;
//n++; // You want to increment to check next number regardless of whether it is dividing both numbers a and b
// So this n++ should be outside if condition
}
n++;
}
System.out.println("The gcd is " + f);
}
}

//} // Extra Bracket
17 years ago
Hi...

Can you write a step wise algorithm that you are using to find the gcd ...
Also solve it for a example...
Say a = 22...b = 44.

Write down the algorithm and then I will help you with the rest...

Thanks,
Megha
17 years ago
Hi...

The details about this APIs mentioned in the K&B book are sufficient for SCJP ...
Thanks,
Megha
[ October 28, 2007: Message edited by: megha joshi ]
In Java the arguments to a method are passed by value.
What does that mean when you pass reference to a object(in this case Stack object)?

A copy of the references s1 and s2 is passed to the method. These references point to the same objects as the original references.

So any change to the references themselves is change to the copy
of the references only...these changes are lost outside the method.

Thus when you do
s2 = s1 inside the method...this is going to point the copy of
s2 reference to a copy of s1 reference inside the method.

When the method returns , the copies are lost as they were local to the
method and now s2 points back to stack s2 which is empty.

Stack s1 on the other hand has one element as we pushed something inside it
which modified the stack object.

Let me know if it isnt clear...

Thanks,
Megha
17 years ago
I am looking for the program/algorithm to implement division without
using divide operator?


I know the subtraction solution...I was looking for a more efficient solution ...something like bits manipulation of some kind?
17 years ago
given N x N matrix of positive and negetive intergers. Write some code
that finds the sub-matrix with teh maximum sum of its elements.

Any ideas?


Thanks
17 years ago
11 questions...in one post often dampen the spirits of a enthusiastic rancher ....too many questions...

I will try answering a few...

1) quote:
--------------------------------------------------------------------------------
The enhanced for loop assumes that, barring an early exit form the loop, you'll always loop through every element of the array.
--------------------------------------------------------------------------------


What this means is that unless you have a break, or continue or exception inside the loop...the loop completes normally...which means it loops through all elements of the collection...

2) object
|
throwable
/ \
Error Exception

Here Object is the Almighty System Object....everything in java is a type of Object...that is why Java is strictly Object oriented...

3)Each method must either handle all checked exceptions by supplying a catch clause or list each unhandled checked exception as a thrown exception

Try not believing it...just code a method with a checked exception...go ahead and complie it...what happens...

The compiler complains..the compiler always checks for the checked exceptions and if you dont handle or declare them...he will keep complaining...so to pacify him you need to do what this statement says...its simply a Java rule.
The Exceptions chapter in K&B give many examples of this..

I will leave the rest of the questions for some one else...

Thanks,
Megha


Now can anyone please help me out with the value of using the "while (b = m.find)" option?
I find it works the same with out the boolean too "while (m.find)"
Is it actually necessary or am i missing something here?



Of Course you are right....You are just using a short hand option...whether you use b = m.find or just use m.find...both evaluate to a boolean value...and as far as its a boolean...all goes well with while loop in this example...

thanks,
Megha
After some more findings I think I cant use the FileInputStream with self buffer or BufferedFileInputStream becuase I want each of the lines of file one by one and process them...and I dont see how I can do it with Low level byte or byte[] IO.

So I am still wondering how do I increase IO performance...
[ June 07, 2007: Message edited by: megha joshi ]
17 years ago


Since I knew the input text was all ASCII, I read bytes directly into buffers. When your read characters, Java has to individually convert each byte to a char - this burns CPU time like crazy.



Ultimately I want to do string lookup and string converstions on each line of the file...can you please elaborate on how I can do this along with reading bytes....and not converting to chars...What is the combination of core API functions you use...
I read somewhere that if I use FileInputStream etc it would be more overhead so using reader or scanner would be good in case of reading strings from text file...

thanks,
Megha
17 years ago
Thanks William,

If you see my first post...I have used threads and queue in the same way as you said...but I guess my IO methods are not efficient...what methods did you use for IO...?

Thanks,
Megha
17 years ago