i need help regarding this in java


 
Thread Tools Search this Thread
Top Forums Programming i need help regarding this in java
# 1  
Old 04-08-2011
i need help regarding this in java

Hi
i'm new to the programming world

i need a java code for following problem

i have an input like
Code:
1 2 3 4 5

the desired output is
Code:
1        2       3     4    5
12      23    34    45 
123    234  345
1234  2345
12345


How can i do that ?
Thanks in advance

---------- Post updated at 12:51 PM ---------- Previous update was at 12:07 PM ----------

import java.lang.String;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author vaibhav
*/
public class tmep {
private static String str;
public static void main(String args[])
{
str="123456789";
for(int lim=0;lim<9;lim++)
{
for(int start=1;start<=(9-lim);start++)
{
for(int i=start;i<=lim+start;i++)
{
System.out.print(str.charAt(i-1));
}
System.out.print("\t");
}
System.out.println("");
}

}

}

Last edited by Franklin52; 04-08-2011 at 03:43 AM.. Reason: Please use code tags
# 2  
Old 04-08-2011
// See If this works only modified your code a bit
//package org.hello.test;


import java.lang.String;
import java.util.Formatter;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author vaibhav
*/
import java.lang.String;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author vaibhav
*/
public class Test {
private static String str;
public static void main(String args[])
{
str="123456789";
for(int lim=0;lim<9;lim++)
{
for(int start=1;start<=(9-lim);start++)
{
// print start number of spaces
int j=0;
while(j<start){
System.out.print(" "); j++;}
//print start-lim number of spaces
for( j=0;j<9-lim-1;j++)
System.out.print(" ");
// now print values from string string
for(int i=start;i<=lim+start;i++)
{
System.out.print(str.charAt(i-1));
}


//System.out.print("\t");
}
System.out.println("");
}
StringBuilder sb = new StringBuilder();
//Formatter formatter = new Formatter(sb, Locale.US);
//formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d")

}

}
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question