Trying To Find 3 Largest Numbers In Array Using Single For Loop
Feb 5, 2014
I am trying to find 3 largest numbers in an array using a single For loop but my following code is still showing randomly sorted numbers.
public class largest3 {
static int m, n, o;
static int array[] = new int[100];
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
array[i] = (int) (Math.random() * 100);
I need to find the largest value in a scanned file.I've gotten the count, sum, average, evens, and odds myself.The code above the while loop is not mine and my professor said I may no edit it or other wise mess with it. I also may not use arrays.Also I've realized that the largest/smallest are recording the value of count. I've tried the following:
Scanner infile = new Scanner ( new FileReader(args[0]) ); int count=0,sum=0, largest=Integer.MIN_VALUE,smallest=Integer.MAX_VALUE, evens=0, odds=0; double average=0.0;
This particular program is supposed to prompt the user to input either an uppercase or lowercase Y to process a phone number, they are then prompted to enter the character representation of a phone number (like CALL HOME would be 225-5466), this repeats until the user enters something other than the letter Y. All of the words entered are to be stored into a single array. The program is then to convert these words in the array to actual phone numbers. Here is what I have so far.
import java.util.*; public class Program1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String begin; int phoneNumber = number.convertNum();
[Code] ....
I realize that the second method doesn't have anything to return yet, I just wanted to put that in there while I was doing things that I actually know how to do ha. The convertNum() method is supposed to be the method with which the array of characters is converted to phone numbers.
I'm still trying to think my way through this. I would think it'd be easier to store the inputs from the user as individual letters rather than words for the sake of converting to phone numbers.
Also, we are only supposed to recognize the first 7 letters of each word that is entered, like the CALL HOME example, there are 8 letters but it's still a seven digit phone number, so I'm not sure how that would work.
Also, when printing the phone numbers after they've been converted, we're supposed to print the hyphen after the third number, I have no clue how that would be done from an array.
This particular program is supposed to prompt the user to input either an uppercase or lowercase Y to process a phone number, they are then prompted to enter the character representation of a phone number (like CALL HOME would be 225-5466), this repeats until the user enters something other than the letter Y. All of the words entered are to be stored into a single array. The program is then to convert these words in the array to actual phone numbers.
import java.util.*; public class Program1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String begin; int phoneNumber = number.convertNum(); System.out.println("Please enter an uppercase or lowercase Y when you are ready to enter a telephone number: ");
[code]....
I realize that the second method doesn't have anything to return yet, I just wanted to put that in there while I was doing things that I actually know how to do ha. The convertNum() method is supposed to be the method with which the array of characters is converted to phone numbers.
would think it'd be easier to store the inputs from the user as individual letters rather than words for the sake of converting to phone numbers.
Also, we are only supposed to recognize the first 7 letters of each word that is entered, like the CALL HOME example, there are 8 letters but it's still a seven digit phone number, so I'm not sure how that would work.Also, when printing the phone numbers after they've been converted, we're supposed to print the hyphen after the third number, I have no clue how that would be done from an array.
import java.util.Scanner; public class AvgLrgSml{ public static void main(String[]args){ System.out.print("Hello there. Please enter any three numbers."); Scanner keyboard = new Scanner(System.in); double num1 = keyboard.nextDouble(); double num2 = keyboard.nextDouble();
The program below is intended to find the arithmetic mean of the numbers stored in the array q in two ways: once by storing the numbers in an ArrayList d, where you allow all the necessary conversions to be performed automatically; and once by storing them in an ArrayList e, where you perform all the conversions by hand. Complete the program.
Here is what I have so far:
double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 }; ArrayList<Double> d = new ArrayList<Double>(); ArrayList<Double> e = new ArrayList<Double>(); for ( double x : q ) { d.add( x ); e.add ( new Double ( x ) );
[Code] .....
Why does it still show "a / d.size?" I thought I fixed that. Whatever, it's supposed to be "dTotal / d.size()", etc.
Write a program that finds both the largest and smallest of several integers. T. For example, a program that determines the winner and loser of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest; the salesperson who sells the least units loses the contest. Write a pseudocode program, then a Java program that inputs a series of 10 integers and determines and prints the largest integer and the smallest integer. Your program should use at least the following four variables: counter, number , largest and smallest. After i run the code i just get the largest input without the smallest .
This is my code :
import java.util.Scanner; // program uses scanner // main Class begins public class Largest12 { public static void main(String[] args) //main method begins { //Initial variables int counter = 0; // initiate counter int number=0 ;
I need a phonebook that allows the user to input up to 100 numbers but stop when they want to. I have the code for the array and input and output. I thought I had the do...while loop correct for asking the user if they want to input another name but its not working, even when you input "N" it still keeps going. Here's my code.
import java.util.Scanner; import java.util.ArrayList; public class PhoneEntry2 { int x; char repeat; final int maxContacts = 100; String[] pNums = new String[100];
I think the problem in this is that the variable max is initialised as 0. Afterwards it remains in the while loop only, so the output is always 0. I dont know how to bring the last max value out of loop and print it.
depth is a variable for the row in the triangle. My problem is that i need the solution to be recursive and i am having trouble doing this. So far i have
public static int findMax(int[][] array,int depth) { if (depth==0) return array[0][0]; else if }
I am trying to write a code that asks the user to input ten numbers and then finds and displays the smallest number out of the ones given. I am supposed to implement arrays into the program to do this. But the problem I have run into is that when I compile the code in jgrasp, I am given several error messages and I am not quite sure what I have done wrong. I'm assuming it is either a syntax or a logical error on my part but reading over the code I do not understand what is causing these errors.
This is the most current draft of my code:
import java.util.Scanner; public class Exercise7_9 { public static void main(String[] args) { double[] numbers = new double[10]; //Enter ten double numbers: Scanner(System.in) java.util.Scanner input = new java.util.Scanner(System.in); System.out.println("Please enter ten numbers: ");
[Code] ....
/* Sample Run: Enter ten numbers: 1.9, 2.5, 3.7, 2, 1.5, 6, 3, 4, 5, 2 */
And these are the exact error messages:
----jGRASP exec: javac -g Exercise7_9.java Exercise7_9.java:35: error: '.class' expected if (double m > list[i]) { ^ Exercise7_9.java:35: error: illegal start of expression if (double m > list[i]) {
I have to get the user to enter in 10 numbers and with those 10 numbers I have to find the total, average, smallest, largest numbers in the set the user inputs. I have the total and average already figured out but how would you go about trying to find the largest and smallest numbers within this set of code.
import java.util.*; public class testhw7 { public static void main (String [] args) { Scannernumberin = new Scanner (System.in); doublevalue[]; doubletotal;
I have got a pretty good framework working for my hangman game so far, however I am quite stumped on how to find multiple instances of the same letter in a single string. I am using indexOf to find the instance of a character in a string, but it only returns the first instance. So if the words(s) contain(s) multiple instances it doesn't register the rest.
public static void main(String[] args) { String word = "crazy horse"; String answer=""; String space=" "; String dash="-";
Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.
Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.
package kickstarter9; public class Kickstarter9 { public static void main(String[] args){ double myList; double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3}; // find the largest value in the list
I need to create a program that uses ArrayList to store integers the user inputs, then scan the array for the largest number. I would also like the user to be able to exit this loop if the number 0 is entered.
As you can see below, I'm not sure how to correctly exit the do-while loop. I found this on another forum, but it does not work.
Java Code:
import java.util.*; public class array { public static void main(String [] args){ ArrayList<Integer> list = new ArrayList<Integer>();
I know it is possible to do this using the Array class but I don't want to use that. I am trying to use selection or bubble sort but I believe selection is what I want. This program asks the user to input 2 arrays with 10 integers each, sorts them from smallest to largest, then states whether they are the same or not. Comparing the two arrays.
import java.util.Scanner; public class SetDifference { public static void main (String args[]) { Scanner sc = new Scanner (System.in); int array1[] = new int[10]; int array2[] = new int[10];
This is what I have to create : Write a method that returns the largest object in an array of objects. The method signature is:
public static Object max(java.lang.Comparable[] a)
All the objects are instances of the java.lang.Comparable interface. The order of the objects in the array is determined using the compareTo method.
Write a test program in the main method that creates an array of ten Strings, an array of ten Integers, and an array of ten java.util.Dates, and finds the largest string (i.e. in the alphabetical order), Integer, and Date in the arrays.
Name your java class Max and your java file Max.java.
I am struggling a bit with this code as I am sure you can see, and am at loss. I have never used the compareTo method. Am I doing this right, or on the right track with my code?
public class Max implements Comparable { public static Object max(java.lang.Comparable[] a) { java.lang.Comparable tempObj = null; for (int i = 0; i < a.length; i++) { if (a[i].compareTo(tempObj) > 0)
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++)
A java program to print 10 random numbers between 512 and 1024 and then finds the smallest and biggest numbers from the list using only one loop
class BSLab4d { public static void main (String[] args){ int[] nums = new int[10]; int largest = nums[0]; int x = 0; int smallest = nums[0]; int y = 0;
I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.
now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.
I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.
[ public class Array1 { public static void main(String[] args) { int array [ ] = new int[100]; for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 26);