Bot J.
asked 06/06/22Java programming
ip = 11,12,13,14,15,16,17,18,19,20 // as a String
op
11
12 15
13 16 18
14 17 19 20
1 Expert Answer
Here is one answer. It works for this very specific case, but could probably be expanded to a more generic use case. To make things easier, I just hard-coded the input rather than try to read it in from a file or type it in each time. Either option would be fairly easy to implement, but would require more code and then various error checking to make sure the input was the right length, type, etc.
I created the output sideways to make it easier to loop through the input string and save it in the output string. This saved having to implement various formulas and if statements to calculate the input and output positions When printing, it is a simple matter of two loops for printing the output.
public class App {
public static void main(String[] args) throws Exception {
int mysub = 0; int mincol=0;int maxcol=3;
int maxrow=4; int outloop=0; int inloop=0;
String[] instring = {"11","12","13","14","15","16","17","18","19","20"};
String[][] outstring= new String[maxrow][maxrow];
for(outloop = 0;outloop<= maxrow; outloop++ )
{
for( inloop = mincol;inloop <=maxcol; inloop++ )
{
//System.out.println(instring[mysub]);
outstring[inloop][outloop] = instring[mysub];
mysub++;
}
mincol++;
}
for(outloop = 0;outloop< maxrow; outloop++ )
{
for( inloop = 0;inloop <maxrow; inloop++ )
{
if(outstring[outloop][inloop] != null)
{System.out.print(outstring[outloop][inloop] + " ");}
}
System.out.println("");
}
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Judy R.
Hello I am 22 years experienced Online Tutor and Assignment Helper for Computer Science and Math. I have been teaching IT Professionals, students from different grades, and Graduate and Post Graduate students for more than 22 years. I am ready to help you with your learning requirements, assignments, tests, and projects. For further discussion about the assignment or project help you need, please add me on skype and my skype id is nettuitions Thanks01/22/23