Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help, please. Thanks Java Programming. My code is not running. What did I do wrong, please? Create a class ArrayListTest . Examples: TomArrayListTest

I need help, please. Thanks

Java Programming. My code is not running. What did I do wrong, please?

Create a class ArrayListTest . Examples:

TomArrayListTest

SueArrayListTest

CindyArrayListTest

Etc.

This class is to contain:

A method that receives an ArrayList populated with an Integer data type holding the integers received from user input.

The user input is to accept Integers that are then assigned to the ArrayList until a value of 0 is entered, which is also assigned to the ArrayList.

The ArrayList is then to be sent to the method.

The method is then to return the largest value in the ArrayList.

If the ArrayList is sent in empty, the method will then return 0.

The method signature is to be: public static Integer max (ArrayList list).

Write additional code for testing your method.

The method will return the largest value that is displayed to the user.

Here is my code My code is not running. What did I do wrong, please?

import java.util.*;

public class ArrayListInteger {

public static void main(String[] args) {

//taking user input

Scanner sc = new Scanner(System.in);

System.out.println("Enter integer numbers or 0 to stop");

int input = sc.nextInt();

//creating arraylist

ArrayList list = new ArrayList();

//adding elements to the arraylist until user input is 0

while(input!=0) { list.add(input); input = sc.nextInt(); }

list.add(input);

//printing maximum value in the list

System.out.println("The maximum value is " + SuArrayListTest.max(list));

sc.close(); } }

class SuArrayListTest{

public static Integer max(ArrayList list)

{ //check if arraylist is empty

if (list.size() == 0) { return 0; }

//finding maximum value in the list

Integer max = list.get(0);

for (int i = 0; i < list.size(); i++)

{ if (list.get(i) > max) { max = list.get(i); } }

return max; }

}

Here is the output and the error I am having

Enter integer numbers or 0 to stop 9,6,8,0,3,2 Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at ArrayListInteger.main(ArrayListInteger.java:8)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

a sin(2x) x Let f(x)=2x+1 In(be)

Answered: 1 week ago