File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes program correction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "program correction" Watch "program correction" New topic
Author

program correction

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
import java.io.*;




public class Pyramid2007
{

public static void main(String args[])
{

try {
int n,i,j,k,m;


Integer iv=new Integer(args[0]);
n=iv.intValue();
for(i=1;i<=n;i++)
{
System.out.print("\t\t\t");
for(k=1;k<=i;k++)
{
System.out.print("\b\b");
}
//m=i*2-1;
for(j=1;j<=n;j+=1)
{
System.out.print(j+" ");

}
System.out.println("");
} } catch(Exception e) { System.out.println("enter positive numbers only(no Strings)"); }
}

}


// hi this program should print
// for input 1
// 1
// for input 2
// 1
// 1 2
// can anyone correct this please
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
anyone can help please
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1367
    
  18
Hi,

Please take a look at some tips for getting the most out of JavaRanch.

If I understood correctly, you are asking how to make a program that will give the following output:



if the program is supplied with an argument at the command line such as this:



Is this right?
[ July 17, 2007: Message edited by: Katrina Owen ]
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
yes
satabdi das
Greenhorn

Joined: Jul 17, 2007
Posts: 28
Hi,

I have got the output, this is the program -->

public class Pyramid2007
{

public static void main(String args[])
{

try {
int n,i,j,k,m;


Integer iv = new Integer(args[0]);
n = iv.intValue();
for(i=1;i<=n;i++)
{
//System.out.print("\t\t\t");
for(k=1;k<=i;k++)
{
System.out.print(" " + k);
}
System.out.println("");
}
} catch(Exception e) { System.out.println("enter positive numbers only(no Strings)"); }
}

}
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1367
    
  18
Okay - in your code, there are many for loops.

Could you try rewriting the code so that it only uses only one for loop?

If not, where do you get stuck?
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1367
    
  18
Satabdi Das,

Please do not just answer with code. This robs the poster of the original question of a great learning opportunity!

Kind regards,
Katrina
Jon Anslow
Greenhorn

Joined: May 05, 2006
Posts: 16

Hi prasanna,



I noticed you are creating an Integer object here. One tip that may prove useful would be to use the Integer.parseInt() method. This would save you creating this Integer object as it is not in use in the rest of your code. eg:


Hope this is useful,
Jon
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
i want the output in pyramid shape
vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170
Window screen 360 X 80 ....
So there are 80 across the page ...
make a for loop that prints space
till the middle of the page....
Then decrement it by whatever you want ....
For each print on a line

For eg:-

--------------------------------------------------------------------
--------------------------------1 (40 spaces)
-------------------------------1 2 (39 spaces)
------------------------------1 2 3(38 spaces)

Hope you got it
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
if i want the logic of the program in a separate method other then main .

how can i do it?
vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170


try to code it and if there is any problem we can help you ..
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
public class Prime100 {


public static void main(String[] args) {


Integer iv=new Integer(args[0]);
int num=iv.intValue();

test(num);

}

public static void test(int num) {

try {
int first;


if(num<2)
{
System.out.println("there are no prime numbers upto 2");

} else {

first=2;
System.out.println(first); }

for(int i=3;i<=num;i=i+2)
{
int flag=1;
for(int j=2; j<=i/2;j++) {
if((i%j)==0)
{
flag=0;

}

}
if(flag==1)
{
System.out.println(i);
}

} } catch(Exception e) { System.out.println("please enter number above 2 to find prime numbers upto that range"); }

}

}

/*********** i tried the code above it worked but when i entered no command line arguments... i didnt get the message in the catch block why? *********/
vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170
The problem is that Exception block is never reach ..
If you want to print that you can do like this



In your previous code,the catch exception is in the other method but as soon as the statement
Integer iv=new Integer(args[0]);
is reached it throws an Exception in the main class..it do not go to the method that you are using
so thats why its was not printing wat you want...

Better way is


You can do all you checking in the main class then call you method test if everything is ok. Anyway this is just an opinion of mine ...


[ July 17, 2007: Message edited by: vanlalhmangaiha khiangte ]
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
public class Pyramid20
{

public static void main(String args[])
{
Integer iv=new Integer(args[0]);
int n=iv.intValue();
test(n);
}

public static void test(int n){


try {
int i,j,k,m;



for(i=1;i<=n;i++)
{
System.out.print("\t\t\t");
for(k=1;k<=i;k++)
{
System.out.print("\b\b");
}
m=i*2-1;
for(j=1;j<=m;j+=2)
{
System.out.print(j+" ");
if(j==m)
{
m=m-2;
for(j=m;j>=1;j-=2)
{
System.out.print(j+" ");
}
break;
}
}
System.out.println("");
} } catch(Exception e) { System.out.println("enter positive numbers only(no Strings)"); }
}

}


/***** but this program is similar to previous one. but i dont have the problem here... what may be the reason? *******/
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
no same problem in both places.

so you mean to say i cant catch in the method because it is thrown in main so that has to be caught before executing the method

but vice versa is possible no?
say the exception was thrown in method which was not caught there but caught in main
vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170
If exception is thrown in the method :-

If in the method there is no catch statement and if the main statement has the catch statement then it will propagate and the catch block in the main will be execute.

If in the method there is specific catch statement for that exception then the catch block of the method will be execute not the catch block of main.

If there is catch block in method,but nothing is specific to that exception and there is a specific catch block for that exception in the main then that specific catch block in the main will be execute
[ July 17, 2007: Message edited by: vanlalhmangaiha khiangte ]
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1367
    
  18
Hi prasanna,

I think maybe there are several questions that are getting in each other's way. I'd suggest removing the problem of exceptions for now, and working out how to get your output in pyramid form.

One way of doing this would be to structure your program something like this:



For your pyramid, you only need enough whitespace to correspond to the longest line of numbers. That number is the same number as the one passed to your program at runtime.

Could you use that to devise a method for padding your pyramid?
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
hi katrina i finished the program thanks everybody for your suggestions.

all suggestions for my improvement on coding is welcome.
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
i am fine with one for loop but when it comes to nested loops ie one for loop within another then i get confused

ie i get the logic only when somebody else writes the code. i cant write it on my own.

guys how do you think when approaching a problem of say pyramid which i asked.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1367
    
  18
I often approach things by trying to solve only a small piece first.

With the pyramid, my first idea would be
I need to add one number to each line

And then I could try something like this:



Then I would tackle the next problem, to make a pyramid rather than a triangle, by figuring out how much whitespace to put before each line.

Then I would probably discover that I had forgotten to think about what would happen if I used numbers with two digits and I would need to figure out how to factor in more whitespace in those cases.

Then I would look at what I had done above, and probably find several ways to simplify it and make it easier to read.

Then I would start worrying about what would happen if someone entered negative numbers, or something that isn't a number, or more than one argument on the command line, and things like that.

Sometimes after working on something, I think of a different way of solving it, and try that as well, to see if it makes the code less complex.

The approach really depends on the problem, as well as how much time you have available to spend on it.

Just keep trying to solve new problems, and don't hesitate to come and ask for suggestions on how to approach whatever you are facing!
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
Thanks katrina and everybody for your suggestions


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: program correction