6 Replies - 1236 Views - Last Post: 08 June 2010 - 04:39 PM Rate Topic: -----

#1 suns021030   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 09

About nested for loop

Posted 07 June 2010 - 08:57 PM

54321
$4321
$$321
$$$21
$$$$1

for (int i=1 ; i<=5 ; i++ {
Question?
}

How to write next for loop to control number and $ sign ? :helpsmilie:
Is This A Good Question/Topic? 0
  • +

Replies To: About nested for loop

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: About nested for loop

Posted 08 June 2010 - 04:55 AM

Moved from Java Tutorials
Was This Post Helpful? 0
  • +
  • -

#3 webpeater   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: About nested for loop

Posted 08 June 2010 - 05:06 AM

View Postsuns021030, on 08 June 2010 - 03:57 AM, said:

54321
$4321
$$321
$$$21
$$$$1

for (int i=1 ; i<=5 ; i++ {
Question?
}

How to write next for loop to control number and $ sign ? :helpsmilie:


This might do the trick.
String numbers = "54321";
String output = "";
for(int i = 1; i <= 5; i++) {
    output += repeat("$", i-1);
    output += numbers.substring(i-1);
    System.out.println(output);
}



For repeat method take a look here

This post has been edited by webpeater: 08 June 2010 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

#4 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: About nested for loop

Posted 08 June 2010 - 08:08 AM

You wouldnt need a nested for loop.

It would be easier to do something like this

String a= "54321";
     for (int i=0 ; i<=4 ; i++) {
       System.out.println(a);
       a = a.replace(a.charAt(i), '$');
	}



Edit - Forgot semi colon.

This post has been edited by m-e-g-a-z: 08 June 2010 - 12:00 PM

Was This Post Helpful? 2
  • +
  • -

#5 webpeater   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: About nested for loop

Posted 08 June 2010 - 12:11 PM

A mutch easier solution than mine ;)
Was This Post Helpful? 0
  • +
  • -

#6 NoobKnight   User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: About nested for loop

Posted 08 June 2010 - 12:54 PM

Here's another solution, which uses nested loops.
    public static void main(String[] args) {
        String a= "54321";
        for (int i=0 ; i<5 ; i++) {
          // put another forloop here to pad the $ signs, think in reverse here, so your incrementer would be -- instead of ++
          // put another forloop here starting with the value of i and display one character at a time.


This post has been edited by NoobKnight: 08 June 2010 - 12:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: About nested for loop

Posted 08 June 2010 - 04:39 PM

webpeater and m-e-g-a-z order Gentlemen
this sounds like homework 10 miles around
Unless the OP certifies that this is not the case we don't do that type of simple assigment

This post has been edited by pbl: 08 June 2010 - 04:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1