Highest Value In Array List?

Feb 2, 2015

I have 3 classes Pet, Cat and Dog classes. Cat and Dog classes are childs of Pet class.

Each pet has the properties of

private String myName;
private int myX;
private int myY;
private int mySpeed;
private int myAge;

I have an array list which creates 100 dogs. How can I print out the dogs with the two highest age values?

View Replies


ADVERTISEMENT

How To Get Highest Value Of Array Of Integers

Jul 1, 2014

I am trying to write a method that returns the busiest hour in a logAnalyzer class that read web server data and analyze hourly access patterns and stores them in an array. My problem is, in order to get the busiest hour, I need to go through the hourCounts array to find the element with the biggest count.

View Replies View Related

How To Make A Method That Find Highest Value In Array

Dec 5, 2014

I made a method which takes values from a data set and finds out which one is the highest value. When it finds the highest value, it returns the country which is associated with the value. Here's the data set.

"Country""Total CO2 2005 (million tonnes)""Road CO2 (million tonnes)""Road CO2 per person (tonnes)""Cars per 1000 people"
10
USA5951.131530.35.16777
UK2573.4119.681.99470
Italy476.08116.862592
Germany841.78150.211.82550
Canada553.02123.423.82562
France414.03128.132.04477
Russia1575.44114.690.8178
Japan1254.47224.241.76447
China5100.6228.020.317
India1147.4691.060.18

So if the number was 5951.13 then the program would return USA. How do I do that? I've already started on trying to get this code to work but it doesn't seem to compiler so what's wrong with it?

public static CO2Data highest (CO2Data [] arr2){
Scanner sc = new Scanner(System.in);
CO2Data highestindex = arr2[0];
CO2Data currentcountry = arr2[0].getCountry(sc.nextLine());

[Code] ....

Also the array is a CO2Data array which is part of the following class:

public class CO2Data { //The CO2Data class will be called in other methods
 
private String country; //A private variable will prevent other users from accessing and changing these variables.

private double totalCO2;
private double roadCO2;
private double CO2PerPerson;
private int carsPerPerson; 
public CO2Data() {
country = "";//this sets the initial values for the different variables

[code].....

View Replies View Related

Comparing Elements In Array To Find Highest Int

Sep 3, 2014

So in this program, which is a grading program, I am trying to compare all the students averages to find who has the highest one and list the grades and the student's names from least to greatest. Yes, I see there are other problems in the program but it is nowhere near finished.

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] studentName = new String[20];
int[] studentAverage = new int[20];
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

Calling Methods For Java GradeBook - Calculate Highest And Lowest Grades In Array

Apr 15, 2015

In this project each individual will create a data analysis program that will at a minimum,

1) read data in from a text file,
2) sort data in some way,
3) search the data in some way,
4) perform at least three mathematical manipulations of the data,
5) display results of the data analysis in numeric/textual form, and
6) display graphs of the data. In addition,
7) your program should handle invalid input appropriately and
8) your program should use some "new" feature that you have not been taught explicitly in class.

(Note: this is to give you practice learning new material on your own - a critical skill of today's programmer.) If you do not have a specific plan in mind for your project, below is a specific project that meets all of the qualifications as long as 7) and 8) are addressed in the implementation.

Everything is done except I need to call my methods in my GradeTester.

GradeBook:

/**
*This class creates an array called scores.
*This class determines the length of the array scores and determines the last grade in the array scores.
*This class sorts the array using a bubble sort, and searches the array.
*This class calculates the mean, standard deviation, and the median of the grades in the array scores.
*Once the grades in the array is sorted, the class then calculates the highest and lowest grades in the array.
*/

public class GradeBook {
public final int MAXARRAY_SZ = 20;
double [] scores = new double [MAXARRAY_SZ];
int lastGrade = 0;
double mean = 0;

[Code] ....

View Replies View Related

Formatting Array List?

Oct 16, 2014

How do you format an arraylist?

Mine looks like this:

[<?xml version="1.0" encoding="UTF-8" standalone="no"?> <DefEnv id="Dev">, <Envt id="Test">, , <DB id="DM">,

But I want it to look like: I'd prefer if the '[' , '<>' and ',' were not on them also but I'm not too bothered about that bit.

[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<DefEnv id="Dev">,
<Envt id="Test">, ,
<DB id="DM">, ]

View Replies View Related

Cannot Read Into Array List

Apr 5, 2014

I am creating a hangman game and I want to read in a list of words from a text file, but after the user inputs the name of the text file. I get 'Exception in thread "main" java.lang.NullPointerException'.

Here is the code, where I think the problems lie.

public void runModel(){
ArrayList<String> pirateWordsList = new ArrayList<String>();
System.out.println("What is the name of the file you would like to load? (The file included is called piratewords.txt'");
Scanner in=new Scanner(System.in);
String file=in.next();
load(file);

[Code] ....

The full error message is this:

Exception in thread "main" java.lang.NullPointerException
at uk.ac.aber.dcs.pirate_hangman.Model.load(Model.jav a:108)
at uk.ac.aber.dcs.pirate_hangman.Model.runModel(Model .java:45)
at uk.ac.aber.dcs.pirate_hangman.Main.main(Main.java: 6)

View Replies View Related

Alternatives To Array List?

Nov 23, 2014

Im trying to create a program in which I read line by line the contents of a text file, and then report each letter along with its frequency. I was wondering how to read through the lines and process it so that my program knows to increase by a number each time a letter appears in my text file. For example, if A appears in my file 5 times, B 3 times, and C 0 times I want to eventually print out

A -- 5
B-- 3
C-- 0

My first thought was to do this using array lists but is there any way I could do this without using one?

View Replies View Related

Why Array List Is Needed

Apr 28, 2015

method called []getLetterGrades but the only hint My professor told me was that I needed to declare another array list for this method and he wouldnt tell me anything else so bummer. But I don't understand why if what we are returning is a char. It would make sense to return an array list of char to get letter grade. Which is what i did but since the function is a char, the array list character wont work as a return.Primarily i would like to know the type that is needed. I just want an explanation for an array list in this method and how it would serve in this method.

import java.io.File;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

[code]....

View Replies View Related

Sort Both Array List

Jun 29, 2014

Directions: public static void initialize(ArrayList names, ArrayList sores)

You should write a function that sorts both array lists, based on the values in the scores array list. This is one of the more conceptually challenging parts of the assignment. You want to sort the scores array list, and simultaneously alter the names array list so that the names continue to line up with their respective scores by index. In the example data above, when the score 9900 moves to the first element of the scores array list, the name "Kim" should also be moved to the top of the names array list. The function should have the following signature:

I'm having trouble figuring out how to sort the lists.

import java.util.ArrayList;
import java.util.Scanner;
public class Assignment5
{
/**
*/
public static void main(String[]args) {
intializeArrays();

[Code] ....

View Replies View Related

Array List And Method

Mar 28, 2014

I had to write a program that prompts the cashier to enter all prices and names, adds them to two arrays lists, calls the method that I implemented, and displays the result and use 0 as the sentinel value. I am having difficulty coming up with a for loop in the method, I believe I have the condition right but I am having trouble with the statements. I now know that String does not have the get property, but I have only done examples with integers and I am not very good with for loops and wouldn't know how to fix it.

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Double> sales = new ArrayList<Double>();
ArrayList<String> names = new ArrayList<String>();
System.out.print("Enter Number of Customers");
double salesAmount;
System.out.print("Enter Sales for First Customers");
salesAmount = in.nextDouble();
while(salesAmount != 0)

[code]....

View Replies View Related

Removing Duplicates In Array List

Sep 18, 2014

I am stuck on this exercise and I don't know what exactly is wrong. I think it's something with the .remove and the for each loop, but I am not sure.

public class seven {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("aaa");
list.add("brr");
list.add("unni");

[Code] ....

This is what i get

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at seven.removeDuplicates(seven.java:24)
at seven.main(seven.java:18)

View Replies View Related

Enum Type And Array List

Feb 10, 2015

Here I have an enum class

public enum Money{

ONE_PENNY(1),
TWO_PENCE(2),
FIVE_PENCE(5),
TEN_PENCE(10),
TWENTY_PENCE(20),
FIFTY_PENCE(50),
ONE_POUND(100),
TWO_POUNDS(200);

private int coin;
Money(int c) {
coin = c;
}
int showCoin() {
return coin;
}

and for a test class, I need an array list with a couple of coins in it (i.e. ONE_POUND, TWO_POUNDS) and a loop that adds together the values of the coins in the list and prints the result. How can I do this?

View Replies View Related

Input From Console To Array List?

Dec 10, 2014

I have this very simple application just to test console input:

import java.util.ArrayList;
import java.util.Scanner;
public class WriteTester {

[Code]....

When I let it run, only every third entry is put into the array list and I have to hit "enter" three times for the "break" in line 21 to trigger. I cannot find out why.

View Replies View Related

Adding Array To A List - String?

Sep 8, 2014

Im making a simple code to add an array to a List (the code im referring to is <String> )

import java.util.*;
public class L5_ArrayListProgram {
public static void main(String[] args){
String[] things = {"lasers","ghouls", "food", "dark"};
List<String>list1 = new ArrayList<String>();
for(String x: things)
list1.add(x);

My simple question is - what are the <String> ...<String> for? I understand it makes the list1 variable a string, but why is it made like this? do we usualy use <String> when we need to make a variable a String?

View Replies View Related

Convert Integer List To Int Array?

Sep 24, 2008

is there a way, to convert a List of Integers to Array of ints (not integer). Something like List<Integer> to int []?

View Replies View Related

How To Use JOptionPane To Search Array List

Jun 14, 2014

My assignment is to create an array list and compare the total salary of two salespeople. I'm wondering if I can use JOptionPane to select an existing sales person from the list and print their information.

I am not having any trouble with the calculations and comparisons, but I am finding limited resources on "searching" for a specific person with JOptionPane.

Here's what I have so far.

public class SalesPeople {
String personName;
double annualSalary;
double salesAmount;
double percentComission;
public SalesPeople(String xPersonName, double xAnnualSalary, double xSalesAmount, double xPercentComission) {

[code]....

View Replies View Related

Sort Array List Print?

Nov 23, 2014

I am having an issue trying to print different types of arrays using one method. Eclipse tells me to convert my print method for each array type:

change method 'printList <e>' to 'printList(ArrayList<integer>)'
change method 'printList <e>' to 'printList(ArrayList<Double>)'
change method 'printList <e>' to 'printList(ArrayList<Character>)'
change method 'printList <e>' to 'printList(ArrayList<String>)'

I would like to accomplish printing all four different arrays using one print method. How to accomplish it or provide examples or links to examples?

import java.util.ArrayList;
public class SortArrayList {
public static class Sort {

[Code].....

View Replies View Related

Array List Scrolled In Applet

Jan 6, 2015

I have to make an applet that uses an array to list 5 songs and the applet has to scroll the list of song titles, one at a time, each song title should start at the top of the applet and scroll to the middle then, scroll off the right hand side, each new song title should scroll in a different color, the applet should loop, when it gets to the end of the list, start over again at the beginning of the list. the code compiles fine put when I go to run the applet nothing shows up in it and a wall of text appears in the command prompt.

This is the code for the html

<html>
<body>
<applet code = "scroll.class" width=250 height=250>
</applet>
</body>
</html>
]

And this is the code for the program :

import java.awt.*;
import java.util.Random;
import java.awt.Color;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class scroll extends java.applet.Applet implements Runnable

[Code] ...

View Replies View Related

Char Array To List Not Working

Mar 30, 2014

Example code that works:

Java Code: List<String> list = Arrays.asList(strDays); mh_sh_highlight_all('java');

My code that doesn't work:

Java Code: List<char> list = Arrays.asList(userInputChars); mh_sh_highlight_all('java');

Error that I get:

Assignment6_2.java.java:45: error: unexpected type

List<char> list = Arrays.asList(userInputChars);
^
required: reference
found: char

1 error

The entire program (that works, except for the error above):

Java Code:

import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
class Assignment6_2 {
public static void main(String[] args) {

[Code] .....

View Replies View Related

Searching Through Array (List Of Names)

Oct 5, 2014

An array which contain a list of names. I let for a user input and I use that to search through the array. For example lets say the string name "Christian" is stored inside the names array and the user types in Chri, the program looks in the array and finds a name that contains Chri and prints them out. How do I go about doing this?

View Replies View Related

Array List Requiring Variable Not A Value?

Dec 11, 2014

The IDE says an error of "required variable, found value". I thought you need a index value to get a item in the arraylist.

package SuperHero;
import java.util.ArrayList;
public class Driver
{
public static void main ( String args[]) {
//intallizing the supers

[Code] ....

View Replies View Related

Drawing Rectangles From Array List

Nov 13, 2014

The program is where you will click anywhere in the jframe and it will draw a new rectangle. I have altered the code given to us but the problem that I am having is the fact that when I click the rectangle will always be connected to the upper left hand part of the jframe, no matter where I click. The program should create rectangles of the same length and width wherever you click. Here is what I have. I believe I have to change something in the addRectangle method, but nothing that I have tried works.

Java Code:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import java.util.ArrayList;
public class RectangleComponent2 extends JComponent

[Code] .....

View Replies View Related

Array List That Contain String Object

May 5, 2014

Suppose that you have an ArrayList and that it contains String objects. Which declaration of the ArrayList requires that objects retrieved using the get method be cast to Strings before calling a String method?

I. ArrayList a = new ArrayList();
II. ArrayList<Object> a = new ArrayList<Object>;
III. ArrayList<String> a = new ArrayList<String>;

A. I only
B. II only
C. III only
D. I and II only
E. I, II, and III

I know that all of these are ways to declare an Array List, but I am unfamiliar with the last two since I usually just declare my Array Lists with the first option.

View Replies View Related

Highest Of Three Grades

Sep 14, 2014

So I have created this program but I am having a couple of small problems with it. The first problem is that the user inputted numbers should all be on the same line. I have spent hours trying to figure this out and I have looked online but I've had no luck. The second problem is that the "lowest score" should be 42 and not 42.0. I don't understand how to make it so the number is an integer. I have posted the output that i'm getting and the way the output should look like at the bottom.

import java.util.Scanner;
public class Random
{
public static void main(String [ ] args) {
Scanner input = new Scanner(System.in);
double a, b, c;

[Code] .....

This is the output that i'm getting.

Enter three scores: 87
42
94

The average is: 74.33333333333333
The lowest score was: 42.0
The average without the lowest score is: 90.5
The grade is: A

This is the way the output should look like.

Enter three scores: 87 42 94

The average is: 74.33333333333333
The lowest score was: 42
The average without the lowest score is: 90.5
The grade is: A

View Replies View Related

Associating Properties With Array List Elements?

Dec 17, 2014

In my project I need to create a pizza ordering system. I have created a array list of pizza toppings and each pizza topping must have a cost property associated with it, then the user can pick the toppings they want and the cost of each topping will be added up and the total cost of the whole pizza will be displayed. But I don't know how to associate a cost with each topping. I have been told that I can access the cost through a get/set method and store it in a private member variable. But I don't know how to do this?

I've tried to do it, but it hasn't worked. This is the code

public void setCost( double cost){ //my get/set method
this.cost = cost;
}
public double getCost(){
double price = 0;
return price;

[Code] .....

But there is an error on each of the ingrediantCost method lines, saying

The primitive type double of cost does not have a field Pepperoni

...and the same for chicken, ham and onion.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved