Question

In: Computer Science

How do I convert the integers in my 2D array list to output decimals numbers? My...

How do I convert the integers in my 2D array list to output decimals numbers? My program works accepting regular integers, but crashes when decimals are entered into my 2D array list.

Can someone improve it so it accepts decimal numbers? Here is my code:

import javax.swing.*;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class TwoDimArray

{

private int numbers[][];

public TwoDimArray()

{

loadArray();

}

public void loadArray()

{

/*

//loadArray() method loads the users defined filename

//@return returns the file's contents

*/

String fileName = "";

try

{

fileName = JOptionPane.showInputDialog("Enter full file path name:");

}

catch(Exception e)

{

JOptionPane.showMessageDialog(null, "File cannot be open" + fileName + "to read");

return;

}

try

{

Scanner in = new Scanner(new File(fileName));

int row, col;

row = in.nextInt();

col = in.nextInt();

numbers = new int[row][col];

for(int i = 0; i < row;i++)

{

for (int j = 0;j < col; ++j)

{

numbers[i][j] = in.nextInt();

}

}

in.close();

}

catch(FileNotFoundException e)

{

JOptionPane.showMessageDialog(null, "file cannot be opened" + fileName +"to read");

}

}

public int displayMaxValue()

{

int high = Integer.MIN_VALUE;

for(int i = 0;i < numbers.length; i++)

{

for(int j = 0;j < numbers[0].length; ++j)

{

if (numbers[i][j] > high)

{

high = numbers[i][j];

}

}

}

return high;

}

public int displayMinValue()

{

int low = Integer.MAX_VALUE;

for(int i = 0;i < numbers.length; i++)

{

for(int j = 0;j < numbers[0].length; ++j)

{

if(numbers[i][j] < low)

{

low = numbers[i][j];

}

}

}

return low;

}

public static int displayTotal(int[][] array)

{

int total = 0;

for(int row = 0;row < array.length;row++)

{

for(int col = 0;col < array[row].length;col++)

{

total += array[row][col];

}

}

return total;

}

public int displayAverage()

{

return displayTotal(numbers) / grabElementCounter(numbers);

}

public static int grabElementCounter(int[][] array)

{

int count = 0;

for(int row = 0;row < array.length; row++)

{

count += array[row].length;

}

return count;

}

public int displayRowTotal(int row)

{

int total = 0;

for(int col = 0; col < numbers[row].length; col++)

{

total += numbers[row][col];

}

return total;

}

public int displayColumnTotal(int col)

{

int total = 0;

for(int row = 0;row < numbers.length; row++)

{

total += numbers[row][col];

}

return total;

}

public int getHighestValue(int row)

{

int high = numbers[row][0];

for (int col = 1; col < numbers[row].length; col++)

{

if(numbers[row][col] > high)

{

high = numbers[row][col];

}

}

return high;

}

public int getLowestValue(int row)

{

int low = numbers[row][0];

for (int col = 1; col < numbers[row].length; col++)

{

if(numbers[row][col] < low)

{

low = numbers[row][col];

}

}

return low;

}


public static void main(String[] args)

{

TwoDimArray twoDimArray = new TwoDimArray();

System.out.println("Average: "+twoDimArray.displayAverage());

System.out.println("Min value: "+twoDimArray.displayMinValue());

System.out.println("Max value :"+twoDimArray.displayMaxValue());

System.out.println("First Row Total: "+twoDimArray.displayRowTotal(0));

System.out.println("First Column Total: "+twoDimArray.displayColumnTotal(0));

System.out.println("The lowest value in the first row is: "+twoDimArray.getLowestValue(0));

System.out.println("The highest value in the first row is: "+twoDimArray.getHighestValue(0));

} //end main function

} // end array

Solutions

Expert Solution

import javax.swing.*;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class TwoDimArray

{

private double numbers[][];

public TwoDimArray()

{

loadArray();

}

public void loadArray()

{

/*

//loadArray() method loads the users defined filename

//@return returns the file's contents

*/

String fileName = "";

try

{

fileName = JOptionPane.showInputDialog("Enter full file path name:");

}

catch(Exception e)

{

JOptionPane.showMessageDialog(null, "File cannot be open" + fileName + "to read");

return;

}

try

{

Scanner in = new Scanner(new File(fileName));

int row, col;

row = in.nextInt();

col = in.nextInt();

numbers = new double[row][col];

for(int i = 0; i < row;i++)

{

for (int j = 0;j < col; ++j)

{

numbers[i][j] = in.nextDouble();

}

}

in.close();

}

catch(FileNotFoundException e)

{

JOptionPane.showMessageDialog(null, "file cannot be opened" + fileName +"to read");

}

}

public double displayMaxValue()

{

   double high = Integer.MIN_VALUE;

for(int i = 0;i < numbers.length; i++)

{

for(int j = 0;j < numbers[0].length; ++j)

{

if (numbers[i][j] > high)

{

high = numbers[i][j];

}

}

}

return high;

}

public double displayMinValue()

{

   double low = Integer.MAX_VALUE;

for(int i = 0;i < numbers.length; i++)

{

for(int j = 0;j < numbers[0].length; ++j)

{

if(numbers[i][j] < low)

{

low = numbers[i][j];

}

}

}

return low;

}

public static int displayTotal(double[][] array)

{

int total = 0;

for(int row = 0;row < array.length;row++)

{

for(int col = 0;col < array[row].length;col++)

{

total += array[row][col];

}

}

return total;

}

public double displayAverage()

{

return displayTotal(numbers) / grabElementCounter(numbers);

}

public static int grabElementCounter(double[][] array)

{

int count = 0;

for(int row = 0;row < array.length; row++)

{

count += array[row].length;

}

return count;

}

public int displayRowTotal(int row)

{

int total = 0;

for(int col = 0; col < numbers[row].length; col++)

{

total += numbers[row][col];

}

return total;

}

public double displayColumnTotal(int col)

{

   double total = 0;

for(int row = 0;row < numbers.length; row++)

{

total += numbers[row][col];

}

return total;

}

public double getHighestValue(int row)

{

   double high = numbers[row][0];

for (int col = 1; col < numbers[row].length; col++)

{

if(numbers[row][col] > high)

{

high = numbers[row][col];

}

}

return high;

}

public double getLowestValue(int row)

{

double low = numbers[row][0];

for (int col = 1; col < numbers[row].length; col++)

{

if(numbers[row][col] < low)

{

low = numbers[row][col];

}

}

return low;

}

public static void main(String[] args)

{

TwoDimArray twoDimArray = new TwoDimArray();

System.out.println("Average: "+twoDimArray.displayAverage());

System.out.println("Min value: "+twoDimArray.displayMinValue());

System.out.println("Max value :"+twoDimArray.displayMaxValue());

System.out.println("First Row Total: "+twoDimArray.displayRowTotal(0));

System.out.println("First Column Total: "+twoDimArray.displayColumnTotal(0));

System.out.println("The lowest value in the first row is: "+twoDimArray.getLowestValue(0));

System.out.println("The highest value in the first row is: "+twoDimArray.getHighestValue(0));

} //end main function

}

Issue: You have declared array as int so that is the reason you are not able to read double so now I have changed to double so that we can read both int and double


Related Solutions

8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and...
8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 To achieve...
I am implementing a generic List class and not getting the expected output. My current output...
I am implementing a generic List class and not getting the expected output. My current output is: [0, 1, null] Expected Output in a separate test class: List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size 6: [0, 1, null, Three, null, 4]. list.set(3, "Three"); is going to produce a list of size 5 (unchanged): [0,...
I am implementing a generic List class and not getting the expected output. My current output...
I am implementing a generic List class and not getting the expected output. My current output is: [0, 1, null] Expected Output in a separate test class: List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size 6: [0, 1, null, Three, null, 4]. list.set(3, "Three"); is going to produce a list of size 5 (unchanged): [0,...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
Java basic sorting problem Supposed I have a simple array list storing the telephone numbers. How...
Java basic sorting problem Supposed I have a simple array list storing the telephone numbers. How can I sort the numbers in descending order with different ways? Give ArrayList<String> tel = ["11223344", "55442211", "99881122", "99002211", "34446666", "12342353"] I come up a solution using Collections.sort(tel), but it requires a compare method and I have no idea its contents and also the position of the method. Would you suggest 2 or 3 ways and write the code to achieve my purpose?
C++ How do you make a 2d array with a user input. For example, the row...
C++ How do you make a 2d array with a user input. For example, the row and columns of the 2d array will be the same so the program can create a square matrix.
How do i send random numbers in my Amazon SES email? What is the code for...
How do i send random numbers in my Amazon SES email? What is the code for it and where would it go Lambda or SES? Please provide step by step instructions
i want a program in java that finds the shortest path in a 2D array with...
i want a program in java that finds the shortest path in a 2D array with obstacles from source to destination using BFS and recursion. The path must be stored in a queue. The possible moves are left,right,up and down.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT