• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Random Numbers and User Input help needed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am working on a program question from a tutorial website and so have done the following(below) but would like to make some alterations to meet the specifications better. Currently it prints a grid (matrix) and shows the sum of the columns at the bottom. The first change I would like to make is to ask the user to enter the number of columns and rows e.g. if they entered 10 - ten columns would be printed.
The second change I would like to make is to get the program to generate the numbers randomly instead of asking the user to input them. I think you have to use the =rand() to do this but I am struglling to work the rest out.
Could somebody implement the changes for me in the code or show me how to do it.
Thanks
---------------------------------------------------------------------------
import java.io.*;
class PrintMatrix
{
public static void main(String[ ] args)
{
PrintMatrix theApp = new PrintMatrix();
theApp.run();
}

private void run()
{
int[][] b = new int[5][5];
int[] sum = new int[5];
int total = 0;
//Get the info
for(int i = 0; i < b.length; i++ )
{
System.out.println("Enter data for Column " + (i+1));
for (int j = 0; j < b[i].length; j++ )
{
sum[i] += b[i][j] = getInt();
}
total += sum[i];
}
//Print the info
for(int i = 0; i < b.length; i++ )
{
for (int j = 0; j < b[i].length; j++ )
{
System.out.print(b[j][i] + "\t");
}
System.out.println();
}
//Print the sums
for(int i = 0; i < sum.length; i++){
System.out.print(sum[i] + "\t");
}
System.out.println("Total: " + total);
}

private int getInt()
{
int num;
String inStr = null;

try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter integer data: ");
inStr = in.readLine();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
num = Integer.parseInt(inStr);

return num;
}
}
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Hunt:

System.out.print("Enter integer data: ");
inStr = in.readLine();


use the same code above to ask for number of rows and number of columns, after declaring your variables, and create your array/matrix after this step:
System.out.print("Enter integer Column: ");
inCol = in.readLine(); //declare this of course
System.out.print("Enter integer Row: ");
inRow = in.readLine();

for the random numbers you want, you could use:
java.util.Random randy = new java.util.Random();
//or use java.util.Random(System.currentTimeMillis());
//you could put this in a loop to fill up your array
randy.nextInt(); //assign this wherever you need...
this should do it?
regards
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic