Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

java loops

I am tryign to get the following to display and used for expressions.  i imported the string class. It does not seem to be working and I can not compile it. What do i have wrong?
to display:
a
ab
abc
abcd

import java.lang.String;
class test
{
public static void main(String args[])
{

for(int i=a;i>=1;i++)
{

for(int j=i;j>=1;j++)
{

System.out.print(j +" ");

}
System.out.println("");
}
}
}
0
pgmtkl
Asked:
pgmtkl
  • 3
1 Solution
 
CEHJCommented:
>>for(int i=a;i>=1;i++)

You have not defined 'a'

You don't need to import String
0
 
sriki77Commented:
Here is the program

class test
{
public static void main(String args[])
{

 for(int i='a';i<='z';i++)
 {
    for(int j='a';j<=i;j++)
    System.out.print((char)j +" ");
    System.out.println("");
 }
}
}
0
 
CEHJCommented:
For the output you mentioned, all you need is:


class test {
      public static void main(String args[]) {

            for (char i = 'a'; i <= 'd'; i++) {
                  for (char j = 'a'; j <= i; j++)
                        System.out.print(j + " ");
                  System.out.println("");
            }
      }
}
0
 
pgmtklAuthor Commented:
thanks for the help, it worked. I didnt realize i did not need to import string
0
 
CEHJCommented:
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now