HarrJ

Day 16 C

Aug 17th, 2023 (edited)
1,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Day16C {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         String txtIn;
  7.         int  arraySize=0;
  8.         double numTotal=0;
  9.         double[] numList;
  10.         try {
  11.             System.out.print("Enter total items to add:");
  12.             txtIn = sc.nextLine();
  13.             arraySize = Integer.parseInt(txtIn);
  14.         } catch (Exception e) {
  15.             System.out.println("error in array size");
  16.             arraySize = 3;
  17.         } finally {
  18.             numList = new double[arraySize];
  19.             System.out.printf("%nEnter %d numbers:",numList.length);
  20.         }
  21.        
  22.         for (int i = 0; i < arraySize; i++) {
  23.             try {
  24.                 System.out.printf("(%d):",i);
  25.                 txtIn = sc.nextLine();
  26.                 numList[i] = Double.parseDouble(txtIn);
  27.                 numTotal += numList[i];
  28.             }catch (Exception e) {
  29.                 System.out.println("error in array size");
  30.                 arraySize = 3;
  31.             }
  32.         }
  33.     }
  34. }
  35. //------------------------------------------
  36. import java.util.Scanner;
  37.  
  38. public class Day16D {
  39.      public static void main(String[] args) {
  40.         Scanner sc = new Scanner(System.in);
  41.         String[][] txtArray = {
  42.             {"r0c0","r0c1","r0c2"}
  43.             ,{"r1c0","r1c1","r1c2"}
  44.             ,{"r2c0","r2c1","r2c2"}
  45.         };
  46.         int row,column;
  47.         String txtIn;
  48.        
  49.         try {
  50.             System.out.print("row:");
  51.             txtIn = sc.nextLine();
  52.             row = Integer.parseInt(txtIn) - 1;
  53.             System.out.print("col:");
  54.             txtIn = sc.nextLine();
  55.             column = Integer.parseInt(txtIn) - 1;
  56.            
  57.             System.out.printf("Content of row %d%n", row);
  58.             for (int i = 0; i < txtArray[row].length; i++) {
  59.                 System.out.print(txtArray[row][i] + ",");
  60.             }
  61.            
  62.             System.out.printf("%nContent of row %d, column %d: %s%n"
  63.                     , row,column, txtArray[row][column]);
  64.         } catch (Exception e) {
  65.             System.out.println("there is an error");
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment