SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. public class matrices //By Andrew Baumher
  4. {//Purpose: to use 2d arrays as matrices and manipulate them by adding, subtracting, or multiplying matrices.
  5.     public static void main(String[]args)
  6.     {
  7.         String userdebug;
  8.         int row, column;  
  9.         int[][] userArray;
  10.         int[][] manipulatorArray;
  11.         Scanner input = new Scanner(System.in);
  12.         System.out.println("Enter the number of rows in the matrix.");
  13.         userdebug=input.nextLine();
  14.         row=debugger(userdebug);
  15.        
  16.         input.nextLine(); //Clears next line if needed
  17.  
  18.         System.out.println("\nEnter the number of columns in the matrix.");
  19.         userdebug=input.nextLine();
  20.         column=debugger(userdebug); //gets number of columns from user
  21.                
  22.         userArray = getArray(row, column);
  23.                
  24.         for (int x=0; x<row; x++)
  25.         {                  
  26.             for(int y=0; y<column; y++)
  27.             {
  28.                 System.out.print(userArray[x][y] +"\t");
  29.             }
  30.         System.out.println();
  31.         }
  32.        
  33.     System.out.println("You are now done.");
  34.     System.exit(0);
  35.     }
  36.     public static int[][] getArray(int row, int column)
  37.     {
  38.         Scanner input = new Scanner(System.in);
  39.        
  40.         StringTokenizer strt = new StringTokenizer("Hello World");//arbitrary words so it will compile.
  41.         boolean disp =  false;//a work around used to make the display more user friendly.
  42.         int [][] Array = new int[row][column];
  43.         int counter, total = row * column; //likely will not need total, but included just in case.
  44.         counter = total;//sets the remaining numbers needed to fill the matrix to the total.
  45.        
  46.         for (int x=0; x<row; x++)
  47.         {
  48.             if (x==0)
  49.                 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.");
  50.             else if (strt.hasMoreTokens())
  51.             {
  52.                 if (disp==false)
  53.                 System.out.println("Works so Far!");
  54.                 {
  55.                 System.out.println("Excess numbers from the previous line have been placed in the matrix in subsiquent row(s)");
  56.                 disp = true;//prevents phrase from displaying on subsiquent iterations of this loop
  57.                 }
  58.             }
  59.             else
  60.             {
  61.                 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");
  62.                 disp = false;
  63.             }
  64.                    
  65.             for(int y=0; y<column; y++)
  66.             {
  67.                 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
  68.                 {
  69.                     System.out.println("Please enter the remaining numbers to fill this row, or any number of the " + counter + " numbers left to fill the matrix.");
  70.                     disp = false;
  71.                 }
  72.                 strt = new StringTokenizer(input.nextLine());
  73.                 Array[x][y]=toInt(strt.nextToken());
  74.                 counter--;
  75.             }
  76.         }
  77.         return Array;
  78.     }
  79.     /*The debugger class will eliminate excess ints. ideally, I'd use hasNextInt and delete it,
  80.      * but due to quirks in java's language, hasNextInt will return true when using System.In
  81.      * A workaround is to use tokens, but this requires extra coding. thus, this method has been created.
  82.      */
  83.     public static int debugger(String str)
  84.     {
  85.         int x;
  86.         StringTokenizer debug = new StringTokenizer(str);
  87.         x=Integer.parseInt(debug.nextToken());
  88.         while (debug.hasMoreTokens())
  89.         {
  90.             String z= debug.nextToken();
  91.             z= null;
  92.         }                
  93.         return x;
  94.     }
  95.     public static int toInt (String x)
  96.     {
  97.         int y = Integer.parseInt(x);
  98.         return y;
  99.     }
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top