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


ADVERTISEMENT

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 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

Generate 10 Random Integers / Store In Array And Then Calling A Method To Display Array

Nov 8, 2014

So I need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array. The method header for the displayArray method is:

public static void displayArray(int[] array)

This is what I have done
 
public class RandomIntegers {
 static int numbers = 0;
public static void displayArray(int[] array) {
System.out.println(numbers + "Numbers Generated");

[Code] .....

View Replies View Related

Convert Array Of Integers To Array Of Characters And Then Print It Out

Feb 13, 2014

I have double checked this code over and over and I just can't find the problem.

What I'm trying to do is take a file and input it into an 2D array.

Ultimately, I should convert the array of integers to an array of characters, then print it out. The file contains a set of ASCII values.

After printing it out, I should then create methods to manipulate the image produced.

Using 2D arrays is a requirement for this exercise.

I think that somehow I'm overcomplicating this and the solution is a lot more simple than I think, but I can't think of what to change.

The error I am getting is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40
at main.main(main.java:17)

Java Code:

import java.util.*;
import java.io.*;
public class main {
public static void main(String[] args)
throws FileNotFoundException {
String[][] data = new String[22][40];

[Code] .....

View Replies View Related

First 2 Integers Of Second Array Are Always 0?

Jan 20, 2015

import java.util.Arrays;
import javax.swing.JOptionPane;
public class Student {
public static void main(String args[]) {
String [] A =new String [4]; //krijon tabelen e emrave

[Code] ....

It Prints [firstname,secondname,thirdname,fourthname]

and [0,0,7,9]

The problem is that the first 2 integers of the second array are always 0 even if i put another grade like for example 6 or 7...

Our teacher asked to input the names and grades of the students using JOptionPane and then find the MINIMUM grade and how many times this grade is repeated...

View Replies View Related

Array Of Integers - Cannot Be Converted To Int

Dec 18, 2014

I have a method that receives an array of integers and saves the ID of the user inside the array of integers and I keep getting the error: "int[] cannot be converted to int".Here is the code:

public boolean Borrow (String TitleOrAuthor,int id){
int count = 0;
int b1 = 0;
int BookCount [] = new int [b1];
for (int i=0;i<Bcount;i++){
if(Booklist[i].getTitle().equals(TitleOrAuthor))
for(int j=0;j<b1;j++){
BookCount [j]= Booklist[i].getCopies();
BookCount [j]= id;
b1++;

[code]...

View Replies View Related

Two Dimensional Array Of Integers

Nov 7, 2014

I am workinh with a couple of functions that deal with two dimensional (square) arrays of integers, doing things like checking equality, etc. For example, I know that I get an ArrayOutOfBoundsException in the isEqual function, but I don't know why.

public class MatrixUtils {
//This function checks if two matrices are equal
public static boolean isEqual(int A[][], int B[][]) {
for(int i=0; i<A.length; i++) {
for(int j=0; j<A.length; i++) {
if (A[i][j] != B[i][j]) return false;

[Code] ....

View Replies View Related

Sorting Array Of Random Integers

Dec 8, 2014

What the fastest way to sort an array of random integers. My instructor told me to look into an algorithm that uses 2n, but that is all he gave me. I am unable to find anything of the sort (pun intended there) on google, in my text, in my other java books or on here. The only thing that I have come up with are things like mergesort with the exception that the indicies of the Array have to be even. The program that I'm writing takes user input for a minimum to maximum range, the amount of numbers to fill the array in that range, and verbose.

I've tried bubbleSort but it takes forever to sort under the conditions below. I have seen a sorting algorithm that uses n (log n) but didn't really understand how that one worked.

Using the 2n algorithm, I should be able to test the range from 1 - 1000 and have it populate an array of of 1,000,000 random integers. It should be able to complete in a matter of miliseconds.

View Replies View Related

Interpret Array Of Integers And Rearrange It

Nov 6, 2014

This code is designed to interpret an array of integers and rearrange it so that the even numbers come before the odd. But it fails when I run it against my test?

public void evensLeft(int[] array) {
int myFun[] = new int[array.length];
int k = 0;
for (int i = 0; array.length-1 > i; i ++) {
if (array[i]%2==0) {
myFun[k] = array[i];
k++;

[Code] ....

View Replies View Related

Creating Method To Print Array Of Integers

May 7, 2014

I am trying to create a method that takes an array of integers and prints it out using System.out.print. I'm having trouble creating the right way to print it out since I cannot find a way to convert the int array to a string to print it out.
 
public static String printArray(int[] num){
for (int i=0; i<num.length;i++){
String msg = num[i];
}
return System.out.print(msg + " ");
}

View Replies View Related

Two Dimensional Array - Converting Integers To String

Apr 10, 2015

I am pretty new to Java and am just learning about two dimensional arrays. I think that I understand the concept, but I seem to be having trouble adding stuff to my array. I wanted to make an array to hold both strings and integers, but wasn't sure if I could put integers in a string array. So I thought that I would be able to convert my integers to string and then add them. This however causes an error. This is my code(yes its probably not the best):

static String [][] students = new String [14][4];
static int number = 0;
String fName, lName, fullName;
int test1, test2, test3, test4;
String a, b, c, d;

[Code] .....

View Replies View Related

Unable To Print Repeating Integers In Array

Aug 5, 2012

I have a problem where i have to write some code to read through an int array and print out the different integers and how many times they occur. For example:

if the array contained the numbers 1,4,2,3,5,4,4,7,5,4,3,6,8,6,4,

i would need the print to appear something like this

integer: 1, times: 1
integer: 4, times: 5
integer: 2, times: 1

etc

as the array is read sequentialy from element 0 to the end. the program should find the integer value in the first element (increment a counter by 1) and then search all other elements to see if the integer reoccurs (counter++ for each time it reappears). obviously the counter would be the 'times' value in the second column.

My problem is i can get this accomplished yet i cant get the program to recognise when it has already registered an integer in a previous element and skip to the next element looking for a new integer. SO for each element in the array it prints the integer and then searches the entire array for the integer again and increments the counter accordingly. so in essence there is a line printed for each integer the number of times it actually occurs.

I simply wish to have my code to find an integer and the number of times it occurs. and then disregard the elements it has registered the previous integer in.

Also, keep in mind that i am NOT permitted to use any new data structures/arrays for storing/remembering values. I can only use int and double variables throughout.

what i have so far is this: with the array being customerID[] :

Java Code:

int num;
int counter;
for(int count = 0; count < customerID.length; count++)
{
counter = 0;
num = customerID[count];
for(int count2 = 0; count2 < customerID.length; count2++)

[Code]...

The output for the int array 1,1,3,0,3,2,0,4,1,3, looks something like this:

Integer: 1, Times: 3
Integer: 1, Times: 3
Integer: 3, Times: 3
Integer: 0, Times: 2
Integer: 3, Times: 3
Integer: 2, Times: 1

[Code]...

and as you can see lines are repeated for each time the same integer occurs in a new element. i just need my code to skip an element if the integer has been previously encounterd and recorded. i.e i need it to look more like this :

Integer: 1, Times: 3
Integer: 3, Times: 3
Integer: 0, Times: 2
Integer: 2, Times: 1
Integer: 4, Times: 1

with the first encountered integer being checked throughout the array and then moving to the next integer/element which is not the same as any previous.

View Replies View Related

Program That Find Minimum Value In Array Of Integers

Feb 3, 2014

I'm required to create a program that finds the minimum value in an Array of integers.

Here is my approach :

public class Person {
public static void main(String [ ] args){
int theArray[]=new int [10];
int result = 0;
for (int i:theArray){
if (i < Integer.MAX_VALUE)

[Code]...

I can't really progress from this position. I keep getting told to change the return type to int on main method, then when i do it says change it to void..

View Replies View Related

Java - Find Number In Given Array Of Integers

May 2, 2014

I wrote this simple piece of code to find the number in the given array of Integers. The program works fine although it shows a couple of errors when i modify it a little bit.

public class DemoLabel{
public static void main(String [] args){
int[][] arrofInts={
{32,45,67,87},
{23,44,55,66},
{12,47,87,56},
{23,44,12,78}

[Code] .....

In the disp() method,Suppose i only want to pass two arguments i.e disp(i,j) given that searchFor variable is visible in the entire class but when i use disp(i,j) i get the following error

DemoLabel.java:38: error: cannot find symbol System.out.println("The number "+searchFor+" is at the location"+i+" , "+j);
^
symbol: variable searchFor
location: class DemoLabel

So i decided to declare the

int searchFor
as
public int searchFor

But the compiler threw another error saying that it was a Illegal start of expression

Also,when i add a SOP line after the label search: it shows the error

DemoLabel.java:25: error: undefined label: search
continue search;
^

Although i got the program to work it would be useful if i could understand why it doesn't work with the modifications.

View Replies View Related

Declare And Creates Integer Array That Can Hold 15 Integers

Nov 3, 2014

The main Method

-Create a main method declares and creates an integer array called nums that can hold 15 integers.

-Use a for loop to fill that array with multiples of 3: 0, 3, 6, 9, etc.

-Then use similar for loop to print each value in the array on one line, with each value separated by a single space.

-Compile and run the program to see the result:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42

As you write other methods, you'll also modify the main method to make calls to them. The printArray MethodWrite a method called printArray that accepts an integer array as a parameter. This method does not return a value, and must be declared as static so that the main method can call it. Instead of printing the array in the main method, move that loop into this method. Call the printArray method from the main method. Compile and run the program to verify it prints the sam result as before.Add a println statement so that after printing the array values on one line, it then moves to the following line.Finally, modify the loop in the printArray method so that, instead of using a traditional for loop, it instead uses a for-each loop. Compile and run the program again.

Part III: More Array Methods

The linearSearch Method In lecture we looked at a method that performed a binary search on a sorted array. A much simpler (though much less efficient) search is a linear search, that simply starts at the front of the array and looks at each element in turn until it finds it or reaches the end.Create a method called linearSearch that accepts an integer array and a single int value as parameters. The goal of the method is to find the second parameter (the target) in the array. The method should return a single int representing the index of the target value. This method should not print any output itself. In this method, use a traditional for loop to scan through the elements in the array. As soon as you find the target value, return the index of that value.

If you scan through the entire array without finding the target value, return a -1.Modify the main method to call the linearSearch method and print the results. Call it twice, searching for the value 18 (which it should find) and the value 10 (which it should not). Including the previous activity, the output of the main method should now look similar to this:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42

The index of 18 is 6

The index of 10 is -1

The sumArray Method

The sumArray method should take an integer array as a parameter and return a single integer representing the sum of all values in that array.Use a for-each loop to access each value in the array and compute a running sum. After the loop, return the total.Call the method from the main method, producing the following augmented output:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42

The index of 18 is 6

The index of 10 is -1

The sum of this array is 315

The addValue Method...The addValue method should accept an integer array and a single int as parameters. The purpose of the method is to add the second parameter to EACH value in the array. The addValue method does not return a value, but the elements inside the array will be modified. Call the addValue method from the main method, adding 100 to each element in the array. Then call the printArray method again to see the modified array values:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42

The index of 18 is 6

The index of 10 is -1

The sum of this array is 315 100 103 106 109 112 115 118 121 124 127 130 133 136 139 142

Test a Different Array..Finally, duplicate the content of the main method to perform similar tests on another array. Instead of filling it with multiples of 3, fill it with multiples of 4. And instead of using an array size of 15, use an array size of 20.Modify the values search for to include one that is in the array and one that isn't.Rerun the main method and carefully check the results.If you haven't been doing it all along (which you should), make sure the appropriate class and method documentation is included.When you're satisfied that all methods are working correctly, modify the main method to delete the second array tests.

View Replies View Related

Generate Random 100 Numbers From 0 To 9 In Array - Count How Many Integers There Are

Mar 7, 2014

I'm trying to generate random 100 numbers, from 0 to 9, in an array using Math.random, but it only outputs 0s which is very confusing to me. Also, I need to be able to count how many different integers there are like 0s, 1s, 2s... 8s, 9s.

Here's my code, I only got as far as the array then got stumped on the counting part.

import java.util.Arrays;
public class countDigits {
public static void main(String[] args) { 
//Create random generator and values
int numbers = (int)(Math.random() * 10);
int arrayCount = 1;

[Code] ....

and my results

0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0

View Replies View Related

Array Sorting In Ascending Order - Displaying Integers

Oct 21, 2014

I couldn't where the problem with this code. The question is : (Write a program that reads in nine integers and sorts the value in the ascending order. Next, the program restores the sorted integers in a 3 x 3 two-dimensional array and display the integers as shown in the result.) And this how i did it:

package test1;

import java.io.*;
import java.util.*;
public class test1{
public static void main(String[] args){
int[] hold = new int [9];
Scanner sc = new Scanner(System.in);
System.out.print("Enter 9 integers, press <Enter> after each");
{
for (int i = 0; i < hold.length; i++);

[Code] ....

View Replies View Related

Method To Take Array Of Integers And Rearrange Numbers From Least To Greatest Using For Loops

Apr 22, 2014

I'm making a method to take an array of integers and rearrange the numbers from least to greatest, using for loops.

I'm getting the error "java. lang. ArrayIndexOutOfBoundsException: 8,

Portion of the ArrayMethods class with the sorting method

Java Code:

public static Integer[] sortArray (Integer[] a)
{
int swap;
for (int i = 0; i < a.length; i++)

[code]....

View Replies View Related

Accept Array Of Non-negative Integers And Return Second Largest Integer

May 22, 2015

Write a function that accepts an array of non-negative integers and returns the second largest integer in the array.

Return -1 if there is no second largest.

The signature of the function is int f(int[ ] a)

Examples:

if the input array isreturn{1, 2, 3, 4}3{{4, 1, 2, 3}}3{1, 1, 2, 2}1{1, 1}-1{1}-1{}-1 

In the signature what I understood is, I should write my function with the given signature,

The return type is "int"

method name is "f"

parameter is "a" right ?
 
Writing my doubts beside the particular line in the code

public static void main()  // In the answer why they didn't use the class ?

In main method why they didn't use parameters ?(String[] args)

{
a1(new int[]{1, 2, 3, 4}); // what is "a1" here is it array name ? this line initializing the array ?
a1(new int[]{4, 1, 2, 3});
a1(new int[]{1, 1, 2, 2});
a1(new int[]{1, 1});
a1(new int[]{1});
a1(new int[]{});


static int a1(int[] a) // what is "a" here parameter ? and "a1" is method name ? why they used the array name and method name same ?

{
int max1 = -1;
int max2 = -1;
for (int i=0; i<a.length; i++)

[Code] .....

View Replies View Related

Displaying Random Value Of Integers And Then Finding Missing Integer From Values In Array

Feb 7, 2015

I have a problem where I have to create random integers from 1 to n + 1. When the array is displayed I have to find the missing integer from the values that are displayed and show that value. I have to use O(n^2) and O(n) operations. I have created this code so far but I cannot figure out how to make my values display n + 1. It is only displaying values 1, 2, 3, 4 and 5 in random order. I also cannot figure out how to find the missing value. I have created a boolean displayed statement but can't determine how to use it in this code.

=Java
import java.util.Random;
public class Task6
{
public static void main(String[] args)
{
int[] numbers = new int[7]; //create array of numbers
Random random = new Random();
boolean displayed = false; //boolean value to determine if number was displayed

[code].... 

View Replies View Related

Create Instance Of Array Of Several Integers And Prints Data Based On Methods

Apr 15, 2014

I have to make two classes. The first one crates an instance of an array of several integers and prints data (average, greatest, lowest, et cetera) based on the second class, which contains the methods. I'm having a problem with the syntax for the first class required to use the methods.

Here's a shortened version of what I have right now just based on processing the number of integers in the array (because if I can get just one method properly connected, I could figure out everything else).

Driver

import java.util.Arrays;
public class ArrayMethodsDriver
{
//Creates the ArrayMethods object
public static void main(String[] args)
{
int[] a = {7,8,8,3,4,9,8,7};

[Code] ....

When I try to compile this, I currently get the "class expected" error on the count part.

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

Sorting From Lowest To Highest

Feb 23, 2014

I'm very new to Java and ran into a problem. My results are not in order and I'm not sure what I'm doing wrong.

My results come out like this instead of being in order from lowest to highest: "77 99 44 55 22 88 11 0 66 33"

Here's what I have:

class ArrayIns {
private long[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArrayIns(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet

[Code] .....

View Replies View Related







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