+ 2

Java: I need help to get started on my programming assignment: Write a program that prompts a user to enter data for an array. T

I don't know how to write a code that will allow the user to input ANY primitive data type. I understand how to code the bottom half to for the getTotal, etc. I just need help with creating a code to get the input of ANY primitive data type. I know I will need to use the scanner class. Here is the whole assignment: Write a program that prompts a user to enter data for an array. The user should be able to input ANY primitive data type (int, float, double, long). The program should also have the following methods: • getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array. • GetAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array. • GetHighest. This method should accept a one-dimensional array as its argument and return the highest of the values in the array. • GetLowest. This method should accept a one-dimensional array as its argument and return the lowest of the values in the array.

31st Mar 2017, 11:05 PM
Jean Prathammavong
Jean Prathammavong - avatar
6 ответов
+ 3
use a scanner to get the users input. You can just make the input a type double since the data type the user enters wont matter with the upcasting unless its a character. (unless assignment says not to do that). use the scanner by: *importing it *creating the object *then get input with next() methods ex/ import java.util.Scanner; // top of program //inside methods Scanner scannerName = new Scanner(System.in); double input = scannerName.nextDouble(); // or whatever datatype you wanted to use
31st Mar 2017, 11:54 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
I believe Rrestoring faith's answer is the correct one
1st Apr 2017, 12:56 AM
Heng Jun Xi
Heng Jun Xi - avatar
+ 2
Type isnt defined for a string value, put it in quotes or dont initialize it. you also havent given the scanner object a name. like: String input; Scanner keyboard = new Scanner(System.in); ps: do you only need to use one array for the assignment? like what @hojat said
1st Apr 2017, 2:43 AM
Rrestoring faith
Rrestoring faith - avatar
0
could you please provide an example
31st Mar 2017, 11:43 PM
Jean Prathammavong
Jean Prathammavong - avatar
0
Hello.. here is what I have so far, but it keeps giving me an error and I am not sure what to do: import java.util.Scanner; public class Pass9 { public class static void(String[] args) { String input = type; Scanner = new Scanner(System.in); System.out.println("Enter Data Type: "); input = keyboard.nextLine(); } if (type.equals("INT")) { //Declare array here of the type int System.out.println(" Enter integer: "); } else if (type.equals("DOUBLE")) { System.out.println("Enter double: "); } else if (type.equals("LONG")) { System.out.println ("Enter long: "); } else if (type.equals("FLOAT")) { System.out.println("Enter float: "); } private static int getTotal(int[] iarray) { int total = 0; for(int i=0; i<iarray.length; i++) { total += iarray[i]; } return total; } private static int getAverage(int[] iarray) { int total = getTotal(iarray); return total / iarray.length; } private static int getHighest(int[] iarray) { int highest = iarray[0]; for(int i=0; i<iarray.length; i++) { int index = iarray[i]; if(index > highest) { highest = index; } } return highest; } private static int getLowest(int[] iarray) { int lowest = iarray[0]; for(int i=0; i<iarray.length; i++) { int index = iarray[i]; if(index < lowest) { lowest = index; } } return lowest; } } }
1st Apr 2017, 2:41 AM
Jean Prathammavong
Jean Prathammavong - avatar
- 1
use whiteboard call class that creates array make getters n setters
31st Mar 2017, 11:41 PM
kitsune
kitsune - avatar