• 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

program correction

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyone can help please
 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 16
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want the output in pyramid shape
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i want the logic of the program in a separate method other then main .

how can i do it?
 
vanlalhmangaiha khiangte
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


try to code it and if there is any problem we can help you ..
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi katrina i finished the program thanks everybody for your suggestions.

all suggestions for my improvement on coding is welcome.
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks katrina and everybody for your suggestions


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic