Win a copy of Penetration Testing Basics this week in the Security forum!

# HELP~~

BarC Di
Greenhorn
Posts: 11
Write a program to draw a pattern like the following sample run. Note that the two sides of the right triangle are equal. The input should be limited to [1, 26].

abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
abcdefghijklmnopqrstuvwx
abcdefghijklmnopqrstuvw
abcdefghijklmnopqrstuv
abcdefghijklmnopqrstu
abcdefghijklmnopqrst
abcdefghijklmnopqrs
abcdefghijklmnopqr
abcdefghijklmnopq
abcdefghijklmnop
abcdefghijklmno
abcdefghijklmn
abcdefghijklm
abcdefghijkl
abcdefghijk
abcdefghij
abcdefghi
abcdefgh
abcdefg
abcdef
abcde
abcd
abc
ab
a

Ilja Preuss
author
Sheriff
Posts: 14112
Do you know how to use a for loop?
BTW, you are more likely to get good answers by using a more significant subject...

Greg Neef
Ranch Hand
Posts: 82
perhaps two for loops in conjunction with an array such as:
static final char [] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

public static void main( String [] args)
{
for ( int outer = 25 ; outer >= 0 ; outer-- )
{
for ( int i = 0 ; i <= outer ; i++ )
{
System.out.print( alphabet[ i ] );
}
System.out.println() ;
}
}

Ilja Preuss
author
Sheriff
Posts: 14112
Looks good. You can do the same with one loop and String#substring, though.

Greg Neef
Ranch Hand
Posts: 82
I realised substring was more elegant, but figured if two loops was a mystery I'd spare him the String class methods for the present.
static final String alphabet = "abcdefghijklmnopqrstuvwxyz";
public static void main( String [] args)
{
for ( int i = 26 ; i >= 0 ; i-- )
{
System.out.println( alphabet.substring(0,i));
}
}
[ September 06, 2003: Message edited by: Greg Neef ]

BarC Di
Greenhorn
Posts: 11
THank you so much...I think I have to use for loops for this... If I am not wrong..Thanks anyway.. thank you!!

Ilja Preuss
author
Sheriff
Posts: 14112
You could also make use of the fact that you can treat a char as a number. (I didn't try the following, but I think it should work.)

BarC Di
Greenhorn
Posts: 11
I've tried running but it keeps saying "fatal exception occurred. program will exit." Is there anything I can do? Sorry and thanks...=)

Greg Neef
Ranch Hand
Posts: 82
this works. pretty slick llja.
public static void main( String [] args)
{
for ( int outer = 25 ; outer >= 0 ; outer-- )
{
for ( char c = 'a' ; c <= 'a' + outer ; c++ )
{
System.out.print( c );
}
System.out.println();
}
}

BarC Di
Greenhorn
Posts: 11
thanks thanks!! but what if I want to switch the right angle to the other side of the triangle, what should I do?
And let's say I only want 10 rows instead of the 26, can I program it that way... I mean let' say I pick a number randomly, the program will display the number of rows I want?
[ September 07, 2003: Message edited by: BarC Di ]

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by BarC Di:
thanks thanks!! but what if I want to switch the right angle to the other side of the triangle, what should I do?

So in the second line, you want one leading space, in the third two spaces etc.?

And let's say I only want 10 rows instead of the 26, can I program it that way... I mean let' say I pick a number randomly, the program will display the number of rows I want?

Of course you can! Do you see where the number of lines is encoded in the above code?

BarC Di
Greenhorn
Posts: 11
Originally posted by Ilja Preuss:

Of course you can! Do you see where the number of lines is encoded in the above code?

um....yah?so what do I have to do to let to the codes?sorry I am completely new and really can't do this to save my life...
so in order to let the program display wat ever number I choose i have to change the code?
Let's say I want to have a program that goes:
java alp 20 and it displays 20 lines
and then when I type
java alph 15 it will display 15
up to 26 and anything beyond will exceed and not run.. is that possible? what should i do?
and how can I change the spacing for the program to shift the right angle to the right side instead??

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by BarC Di:
um....yah?so what do I have to do to let to the codes?sorry I am completely new and really can't do this to save my life...

It's really not that hard to learn. Do you have any textbook at hand?

so in order to let the program display wat ever number I choose i have to change the code?

Yes, of course. No big changes, in fact, just some expansions.
But first you have to understand the code, of course. Do you already understand what each line of the code is doing? Could you explain it in your own words?

Let's say I want to have a program that goes:
java alp 20 and it displays 20 lines
and then when I type
java alph 15 it will display 15
up to 26 and anything beyond will exceed and not run.. is that possible?

Yes. Did you notice the args argument to main method? It contains all the command line parameters. So in your first example, args[0] will hold the String "20".
and how can I change the spacing for the program to shift the right angle to the right side instead??

You need to print the correct amount of spaces at the start of each line, so it seems to me. You could use another for-loop for that.

BarC Di
Greenhorn
Posts: 11
I m referring from a book Javaswing that I found in the library..The question was found in another book that I photocopied from the library but it does not provide an answer and I can't seem to find a clue from it on how to do it..Regarding understanding the code, I am not really sure if I really understood it.. but here is my interpretation..Please correct me if I am wrong.

public static void main( String [] args)
{
for ( int outer = 25 ; outer >= 0 ; outer-- ) ---so the limit is 25 alphabets? and it is more than '0', which is 1 and above. -- means decrease 1 each line right?
{
for ( char c = 'a' ; c = 'a' + outer ; c++ ) -----this has got something to do with the variable right? like 'a' is the variable and c++ is the increment?
{
System.out.print( c );------ is there something I should adjust here in order to get the spacing I want?
}
System.out.println();
}
}
what other things do I have to do in order to get the effects I asked previously..When you said args,the command prompt mentioned 'identifier', is there an identifier that I should add?

[ September 07, 2003: Message edited by: BarC Di ]

Greg Neef
Ranch Hand
Posts: 82
Something like this then:
public class AlphaLoop2 {
public static void main( String [] args)
{
if ( args.length == 1 ) // make sure they gave a row count
{
int rowCount = Integer.parseInt(args[0]) ; // Use the Integar class method to get an int from a String
for ( int outer = (rowCount - 1) ; outer >= 0 ; outer-- )
{
for ( char c = 'a' ; c <= 'a' + outer ; c++ )
{
System.out.print( c );
}
System.out.println();
}
}
else
{
System.out.println("Usage: AlphaLoop2 rowCount") ; // show them the correct calling format
}
}
}

BarC Di
Greenhorn
Posts: 11
gee thanks.. that was rather comprehensive..
I am now trying to write something that fetches a string from keyboard and converts it to an integer without using Integer.parseInt() or the like. I would like to skip any tabs or leading spaces, finish processing at the end of the string or when spaces are encountered, and also terminate immediately when illegal characters are encountered after giving an error message.
According to the book, I could use either int length() that returns the length of this string, or
char charAt(int index) that returns the character at the specified index.
So,this is my attempt:
class int{
public static void main (string args [])
{
int i;
String s;
double x;
System.out.print ("enter an integer =>");
System.out.flush();
int=(new Integer (int_string)).intValue();
{
System.out.println("The integer is =>");
}
}
}
The output should look like this:
Enter an integer => +34
The integer is => 34
Enter an integer => 88
The integer is => 88
Enter an integer => -747 9876
The integer is => -747
Enter an integer => 23e
Error: illegal input
Enter an integer => +34
The integer is => 34
Don't think mine wil work.. don't have the courage to try..Can u help me correct my mistakes?