473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Accessing variables to create matrices

How do I create a program that takes user input and prints out a matrix. Ideally I would want to have the use to decide the number of matricies, the number of rows and columns and a sum, multiply, devide of the matricies when they are complete.

Thanks
-Ty

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.io.IOException;
  3.  
  4.  
  5. public class ScannerDemoforNetbeans
  6. {
  7.  
  8.     public static void main (String [] args)throws IOException
  9.     {
  10.    int x = 55;
  11.         Scanner s = new Scanner (System.in);
  12.     System.out.println ("What is your name?");
  13.     String name = s.nextLine ();
  14.      Scanner a = new Scanner (System.in);
  15.     System.out.println ("welcome, " + name + ", would you like to play a game with me?");
  16.     String answer = a.nextLine ();
  17.  
  18.     if (answer.equalsIgnoreCase("yes"))
  19.     {
  20.         System.out.println ("Will you Give me some numbers so I can make a matrix with them?");
  21.         Scanner nextanswer = new Scanner (System.in);
  22.         String q = nextanswer.nextLine();
  23. }
  24.     if (answer.equalsIgnoreCase("yes")) 
  25. {
  26.     System.out.println ("YAY!!! okay, How many matricies do you want");
  27.     Scanner i = new Scanner (System.in);
  28.     int number1;
  29.     number1 = i.nextInt(); 
  30.     System.out.println(number1);
  31.  
  32.    System.out.println ("Okay, you got it! How many rows  do you want");
  33.    Scanner rc= new Scanner (System.in);
  34.    public static rows;
  35.    rows= rc.nextInt();
  36.    System.out.println ("How many columns do you want?");
  37.    Scanner c = new Scanner (System.in);
  38.    public static columns;
  39.    columns = c.nextInt();
  40.    System.out.println ("Rows" + rows);
  41.    System.out.println ("Columns:" + columns);
  42.  
  43.  
  44. }
  45.    // }   
  46.    // public static void createarray (String [] args)
  47.    // {
  48.     int matWidth = ScannerDemoforNetbeans.columns;
  49.         int matHeight = ScannerDemoforNetbeans.rows;
  50.         int maxCellValue = 70;
  51.  
  52.                     int [][] mat1 = new int [matHeight][matWidth];
  53.                     int [][] mat2 = new int [matHeight][matWidth];
  54.                     int [][] sum = new int [matHeight][matWidth];
  55.  
  56.                     for (int i=0; i<matHeight; ++i)
  57.                     {
  58.                         for (int j=0; j<matWidth; ++j)
  59.                         {
  60.                             java.util.Random r = new java.util.Random();
  61.                             mat1 [i][j]= r.nextInt (maxCellValue);
  62.                             mat2 [i][j] = r.nextInt (maxCellValue);
  63.                         }
  64.                     }
  65.  
  66.                     print2Darray (mat1);
  67.                     System.out.println ();
  68.                     print2Darray (mat2);
  69.                     for (int i=0; i<matHeight; ++i)
  70.                     {
  71.                         for (int j=0; j<matWidth; ++j)
  72.                         {
  73.                             sum [i][j]= mat1 [i][j] + mat2 [i][j];
  74.                         }
  75.                     }
  76.                     System.out.println ();
  77.                     print2Darray (sum);
  78.     }
  79.     public static void print2Darray (int[][]arr)
  80.     {
  81.         for (int [] i : arr)
  82.  
  83.         {
  84.           for (int j : i)  
  85.           {
  86.               System.out.print (j + " ");
  87.           }
  88.         System.out.println();
  89.         }
  90.     System.out.println ();
  91.     }
  92. }
  93.  
Dec 20 '12 #1
2 2120
Rabbit
12,516 Expert Mod 8TB
You haven't told us what's wrong with the code.
Dec 21 '12 #2
Anas Mosaad
185 128KB
Your class does a big part of what your task. However, it has some pretty obvious compilation error.

I expect them to be typo errors but if you are new to java, please note that static variables can be defined only as class members. They can never be defined as local variables in a method.
Jan 5 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Leigh Riley | last post by:
Hi, Can someone tell me if the following is possible? I have a file containing some STATIC variables, and some functions e.g. -------------------------------------------------...
2
by: Domestos | last post by:
Hi all, Okay after reading some text on PHP i have come across my first query... What is the best method in using and accessing varibales... my book gives me three methods but gives pro's and...
0
by: James | last post by:
Hi, I am trying to build a shopping cart for my DVD website and am having trouble reading variable over different pages. I have a page that allows the user to add things to their cart and this...
8
by: scott | last post by:
hi, sorry if this has been adressed some where elss. if it has plz can you point me to it and ill go read it. any way, i want to declare some variables with in a header file. lets say int i; ...
4
by: Jim Heavey | last post by:
Hello, I have created a form which I intend to use a a dialog box. It has a few controls on that form and I changed the properties of those controls to "public" with the idea that I could access...
2
by: Nathan Sokalski | last post by:
I would like to access variables and functions that I declare in the Global.asax.vb file. However, I am having trouble doing that. What does the declaration have to look like in the Global.asax.vb...
1
by: Nathan | last post by:
Hi, I have created a class library creating a number of forms and a few public variables. I have a project that references the .dll for this class library, and in that project I need to access...
2
by: Jurek Dabrowski | last post by:
hi all, I have a question in reference to accessing variables in another class maybe someone has dealt with before. I have some public variables declared in my main plug-in class...
1
tolkienarda
by: tolkienarda | last post by:
hi all i have an have a script that i include in my program that validates a form, i was wondering if it would be possible to access variables from that script in my main script. example...
6
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
Is there any possibility that i could access a the variables that i have defined in a function for example def foo(): i = 10 i = i + 10 I would like to retrieve the value and use it in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.