naito_de
asked on
Triangle in Java
i try to write a program in java to display the following but was unsuccesful, it keeps displaying that it is out of range in c promt.
the program needs to look like this.
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
thanks.
the program needs to look like this.
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
thanks.
ASKER
I don't understand the above statement, I am still a beginner in Java. Below is my programme but it does not work, can you kindly advise. Thanks.
class Lab_02c {
public static void main (String[] args) { //keyboard input:an int
int x = Integer.parseInt(args[0]);
if (x <= 26 && x > 0) {
for (int i = 26-x; i < 26; ++i) {
for (int j = i; j < i; ++j) {
System.out.print(" ");
}
char ch ='a';
for (int j = 1; j < 26; ++j) {
ch = ++ch;
System.out.print(ch);
}
System.out.println();
}
}
else {
System.out.println("Size out of range!");
}
}
}
class Lab_02c {
public static void main (String[] args) { //keyboard input:an int
int x = Integer.parseInt(args[0]);
if (x <= 26 && x > 0) {
for (int i = 26-x; i < 26; ++i) {
for (int j = i; j < i; ++j) {
System.out.print(" ");
}
char ch ='a';
for (int j = 1; j < 26; ++j) {
ch = ++ch;
System.out.print(ch);
}
System.out.println();
}
}
else {
System.out.println("Size out of range!");
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
this looks like homework...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for all the comments but its too difficult for me to understand. i have stick with my original programme n found the mistakes.
I'm quite happy to explain it! It's better to start with something that works than doesn't work ;-)
Your code when modifed as the following works
public static void main (String[] args) {
int x = Integer.parseInt(args[0]);
if (x <= 26 && x > 0)
{
for (int i = 26-x; i < 26; ++i)
{
for (int j = 26-i; j < 26; ++j)
{
System.out.print(" ");
}
char ch ='a';
for (int j = 0; j < 26-i; ++j)
{
System.out.print(ch);
ch = ++ch;
}
System.out.println();
}
}
else
{
System.out.println("Size out of range!");
}
}
public static void main (String[] args) {
int x = Integer.parseInt(args[0]);
if (x <= 26 && x > 0)
{
for (int i = 26-x; i < 26; ++i)
{
for (int j = 26-i; j < 26; ++j)
{
System.out.print(" ");
}
char ch ='a';
for (int j = 0; j < 26-i; ++j)
{
System.out.print(ch);
ch = ++ch;
}
System.out.println();
}
}
else
{
System.out.println("Size out of range!");
}
}
>> ch = ++ch;
this is a very confusing line.
you should simply replace it by
>> ch++;
or
>> ++ch;
or
>> ch += 1;
or
>> ch = ch + 1;
this is a very confusing line.
you should simply replace it by
>> ch++;
or
>> ++ch;
or
>> ch += 1;
or
>> ch = ch + 1;
compile this in ur compiler and run as "java Lab_02c 26". u will get what u want with ur same code with errors corrected.
class Lab_02c {
public static void main (String[] args) { //keyboard input:an int
int x = Integer.parseInt(args[0]);
if (x <= 26 && x > 0) {
for (int i = 26-x; i < 26; ++i) {
for (int j = 0; j < i; ++j) // earlier this condition will not print space at all, coz if j=1 it can never be less than i
{ // now this is changed to start from a to whatever is the current loop count.
System.out.print(" ");
}
char ch ='a';
for (int j = i; j < x; ++j) {// it was earlier printing a to z always, but it shud not. it shud only print increment values
System.out.print(ch);
ch = ++ch; // this line was above print statement. so earlier it will start printing only from 'b'
}
System.out.println();
}
}
else {
System.out.println("Size out of range!");
}
}
}
class Lab_02c {
public static void main (String[] args) { //keyboard input:an int
int x = Integer.parseInt(args[0]);
if (x <= 26 && x > 0) {
for (int i = 26-x; i < 26; ++i) {
for (int j = 0; j < i; ++j) // earlier this condition will not print space at all, coz if j=1 it can never be less than i
{ // now this is changed to start from a to whatever is the current loop count.
System.out.print(" ");
}
char ch ='a';
for (int j = i; j < x; ++j) {// it was earlier printing a to z always, but it shud not. it shud only print increment values
System.out.print(ch);
ch = ++ch; // this line was above print statement. so earlier it will start printing only from 'b'
}
System.out.println();
}
}
else {
System.out.println("Size out of range!");
}
}
}
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
-- Split between CEHJ and maheshexp
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
TimYates
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
-- Split between CEHJ and maheshexp
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
TimYates
EE Cleanup Volunteer
int len = s.length();
for(int i = 0; i < len; i++){
System.out.println( s.subString( 0, len - i) );
}