Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class matrices //By Andrew Baumher
- {//Purpose: to use 2d arrays as matrices and manipulate them by adding, subtracting, or multiplying matrices.
- public static void main(String[]args)
- {
- String userdebug;
- int row, column;
- int[][] userArray;
- int[][] manipulatorArray;
- Scanner input = new Scanner(System.in);
- System.out.println("Enter the number of rows in the matrix.");
- userdebug=input.nextLine();
- row=debugger(userdebug);
- input.nextLine(); //Clears next line if needed
- System.out.println("\nEnter the number of columns in the matrix.");
- userdebug=input.nextLine();
- column=debugger(userdebug); //gets number of columns from user
- userArray = getArray(row, column);
- for (int x=0; x<row; x++)
- {
- for(int y=0; y<column; y++)
- {
- System.out.print(userArray[x][y] +"\t");
- }
- System.out.println();
- }
- System.out.println("You are now done.");
- System.exit(0);
- }
- public static int[][] getArray(int row, int column)
- {
- Scanner input = new Scanner(System.in);
- StringTokenizer strt = new StringTokenizer("Hello World");//arbitrary words so it will compile.
- boolean disp = false;//a work around used to make the display more user friendly.
- int [][] Array = new int[row][column];
- int counter, total = row * column; //likely will not need total, but included just in case.
- counter = total;//sets the remaining numbers needed to fill the matrix to the total.
- for (int x=0; x<row; x++)
- {
- if (x==0)
- System.out.println("You can enter all " + total + " numbers in the matrix at once, or several at a time.\nIt is recommended you go by row for your own sanity's sake.");
- else if (strt.hasMoreTokens())
- {
- if (disp==false)
- System.out.println("Works so Far!");
- {
- System.out.println("Excess numbers from the previous line have been placed in the matrix in subsiquent row(s)");
- disp = true;//prevents phrase from displaying on subsiquent iterations of this loop
- }
- }
- else
- {
- System.out.println("This row has been filled. Enter all of the numbers in the next row, or any number of the remaining " + counter + "numbers");
- disp = false;
- }
- for(int y=0; y<column; y++)
- {
- if (disp==true && strt.hasMoreTokens()==false)//occurs if the user input more numbers above the length of a row, but not enough to fill a subsiquent row
- {
- System.out.println("Please enter the remaining numbers to fill this row, or any number of the " + counter + " numbers left to fill the matrix.");
- disp = false;
- }
- strt = new StringTokenizer(input.nextLine());
- Array[x][y]=toInt(strt.nextToken());
- counter--;
- }
- }
- return Array;
- }
- /*The debugger class will eliminate excess ints. ideally, I'd use hasNextInt and delete it,
- * but due to quirks in java's language, hasNextInt will return true when using System.In
- * A workaround is to use tokens, but this requires extra coding. thus, this method has been created.
- */
- public static int debugger(String str)
- {
- int x;
- StringTokenizer debug = new StringTokenizer(str);
- x=Integer.parseInt(debug.nextToken());
- while (debug.hasMoreTokens())
- {
- String z= debug.nextToken();
- z= null;
- }
- return x;
- }
- public static int toInt (String x)
- {
- int y = Integer.parseInt(x);
- return y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement