+ 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
7 ответов
+ 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
0
I really need some Java assignment help. I'm working on a programming task where I have to write a Java program that lets a user input data for an array. The tricky part is — the user should be able to input ANY primitive data type (like int, float, double, or long). I understand how to handle the calculations (like total, average, etc.), but I'm stuck on how to actually get the user input in a flexible way using the Scanner class. Here’s the full 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: Accepts a 1D array and returns the total. • getAverage: Accepts a 1D array and returns the average. • getHighest: Accepts a 1D array and returns the highest value. • getLowest: Accepts a 1D array and returns the lowest value. If anyone’s worked on something like this before or can guide me through handling different data types with user input, I’d really appreciate it Also, if you're stuck like me and need help, I found this site that offers affordable help for programming assignments: www.programminghomeworkhelp.com They’re offering 10% off right now if you use the code SPRING10OFF – super helpful for tight student budgets. Thanks in advance!
26th May 2025, 9:56 AM
Joe Williams
Joe Williams - avatar
- 1
use whiteboard call class that creates array make getters n setters
31st Mar 2017, 11:41 PM
kitsune
kitsune - avatar