Count And Return Number Of Times Integer Occurs In Array
Nov 16, 2014
I have problems getting the right number of times for each number of the array. Here is my code:
public static void main(String[] args) {
int[] a = { 3, 13, 5, 9, 13, 6, 9, 13, 2, 3 };
int num = 3;
int count = numbers(a, num);
for (int i = 0; i < a.length; i++) {
I'm trying to write a program that counts the number of times each number in an array occurs. In theory I understand how it work, you take the first element in the array, compare it again the rest of the array and any time it occurs in the array you increment a counter by 1. Then move onto the next element A[1] and so on...
This is what I've done so far :
public static void main(String[] args) { int[] array = {5,6,2,5,2,2,0,1,6}; int count = 0; for(int i = 0; i <array.length; i++) { int val = array[i]; for(int j = i+1; j<array.length; j++)
[Code] .....
I think I'm on the right track as it correctly prints out the frequency of the first 3 elements, but I'm definitely going wrong somewhere as it just doesn't do it right after that!
Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage.
Write a recursive Java method countDigitx that takes two integer numbers as input (a number n and digit x) and returns the number of times digits x occurs in number n:
For example if n=23242 and x=2 then the method should return 3 that is the number of times the number 2 found in n.
Write a program using a while-loop (and a for-loop) that asks the user to enter a string, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. (So, you will have two separate program codes, one using a while-loop and the other one using a for-loop.)
Example: Enter a string: "Hello, JAVA is my favorite programming language." Enter a character: e The number of times the specified character appears in the string: 3
I don't even know where to begin I've only got this
import java.util.Scanner; public class letterCounter { public static void main(String [] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a string"); String myString = sc.nextLine(); System.out.println("Enter a letter"); String letter = sc.nextLine(); } }
I am trying to flip a coin 1000 times and make the driver class count the number of times a head and tails appear. However, I am having some trouble trying to figure out the error:
package Week5; import java.util.Random; public class Coin { private int heads; private int tails; private int face;
[Code] ....
I was able to print out the generated numbers, but java will print either heads or tails a thousand times and count that as 1000 and nil for the other probability...
I am currently truing to make this class instantiate 100,000 dice rolls of 2 dice. And I also need to keep track of how many times each possible total occurs and I am having trouble outputting the result. Right now when I run my code it is just showing the results of each of the 100,000 roles.
public class ltefera_DiceRollTest { public static void main(String[] args) { ltefera_DiceRoll diceRoll = new ltefera_DiceRoll(10); System.out.println("Total # of pips" + " "); diceRoll.printArray(); System.out.println(diceRoll.countDice(2)); System.out.println(diceRoll.isArrayDataValid()); System.out.println(diceRoll.getTotal()); System.out.println(diceRoll.allDifferent());
Write a Java code of the method startsWithCount that takes an array of strings words and a String S. The method should return an integer that is the number of strings that starts with S.
For example if:words = { "All", "Arab", "size", "Almond", "Allowed", "here"} and S= "All", then the method should return 2
PHP Code:
public class StringwithCount { public static void main (String[]args) { String strings[] = { "All", "Arab", "size", "Almond", "Allowed", "here"}; String output= ""; for ( int i = 0; i < words.length; i++) { if (words[i].startsWith("s")) c + +; }
The game runs fine, however I have to include how many times each letter that is guessed occurs within the word. For example, if the guess is e, the program should print out: The character e occurs in 3 positions. How would I go about doing this??
/* * A program that plays simple word guessing game. In this game the user provides a list of words to the program. * The program randomly selects one of the words to be guessed from this list. * The player then guesses letters in an attempt to figure out what the hidden word might be. * The number of guesses that the user takes are tracked and reported at the end of the game. */
This is my code up to now and I can't do anything to make it work. I want it to tell me how many times the number 3 appears, and the last position it was in. I am getting errors like"Cuanto.java:88: getPosition(double[],double) in Cuanto cannot be applied to (double) ....
a = getPosition(a);" and unreachable statements, and value not found. It's all over the place every time I make a change to it.
Java Code:
public class Cuanto { static int getPosition(int count,double listOfValues[], double targetValue) { int a = 0, i; for (i=0; i < listOfValues.length; i++) { if (listOfValues[i] == targetValue) { a++;
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth. at the moment my code looks like this:
// File1: public class date { public int day; public int month; public int year; }
// File 2: public class monthlength { public int mlong(date X) { int t; t = X.month; if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12) { return 31; } if(t == 4 || t == 6 || t == 9 || t == 11) {return 30;} } }
Question - Given an specific integer and an array of integers, find and remove all occurrences of the given integer and return an amended array. I solved it. Here is my solution -
1. Create a program that will return the maximum and minimum numbers in the elements of ONE-dimensional integer array. Save it as MaxMin_OneDim.java
2. Create a program that will return the maximum and minimum numbers in the elements of each row in a TWO-dimensional integer array. Save it as MaxMin_TwoDim.java
3. Write a program PrintPattern which prompt a user to enter a number and prints the following patterns using nested loops (assumed user entered number is 8 output is:)
1 .... 87654321
12 .... 7654321
123 .... 654321
1234 .... 54321
12345 .... 4321
123456 .... 321
1234567 .... 21
12345678 .... 1
(Without the dots, i just put them to give spaces)
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++)
I am trying to count the number of occurrences of a string in an array list. I used the following code:
int count = Collections.frequency(strings, search);
strings is the name of the array list and search is the string that I am trying to count the number of occurrences of. My program compiles correctly, but when I enter the string to search for, I get the following error: "Exception in thread "main" java.lang.NullPointerException at java.util.Collections.frquency(Collections.java:37 18)
Why am I getting this error message and how do I fix it?
I'm new to Java. I have a problem with this code, wherein you have to count how many times you borrow in order to do subtraction.
For example: x = 312; y = 34;
The output should be: 2 because you have borrowed 2 times in subtraction.
My problem is like this, whenever I input x=12, y=2, the output is 1, also, when x = 12, y = 1. It should be 0.
Here's my code:
import java.util.Scanner; public class BorrowOut{ public static void main (String [] args){ Scanner in = new Scanner (System.in); int x; int y; int i = 0; int j = 0;
public class PairOfDice { private Die die1, die2; public PairOfDice() { die1 = new Die(); die2 = new Die();
[Code] .....
I am struggling with the final part of my code to get the dice to roll 1000 times and tell me how many times a 7 was rolled. I am getting these errors in jgrasp
----jGRASP exec: javac -g PairOfDice.java
PairOfDice.java:34: error: <identifier> expected count = 0; ^ PairOfDice.java:35: error: illegal start of type for (rep = 1; rep <= 1000; rep++) {
I have an array with the following characters {'E', 'L','E','P','H','A','N','T','P','O'}
now, I need an array that will store the first array such that only the occurence occurs e.g {'E','L','P','H','A','N','T','O'} Notice that the characters 'E' and 'P' occur twice and as a result were not repeated the second time in the new array.
How would one go about this using the counting elements technique?
I tried this but not sure how to use the counting elements technique.
char [] arr = new char{'E', 'L','E','P','H','A','N','T','P','O'}; char[] bucket = new char[(arr[0] * arr.length)]; for (int i = 0; i < len; i++) bucket[arr[i]]++;
//read the file //make the numbers 1 string line //count the number of repetitiveness in the string for the numbers //display four lowest ones
import java.io.*; import java.util.*; public class Lottery2
[Code] ....
when I run it the array gets sorted but how do i keep the data in other words
what it is supposed to do is grab numbers from a file, and give me the lowest 4 numbers back. I know the numbers using the array but how do i pull out the lowest 4 and keep the data true no matter what numbers or how many of them are in the file.
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is even return 1, if it is not even return 0. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth.
At the moment my code looks like this:
// File1:
public class date { public int day; public int month; public int year; }
// File 2:
public class monthlength { public int mlong(date X) { int t; t = X.month; if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12) { return 31; } if(t == 4 || t == 6 || t == 9 || t == 11) {return 30;} } }
So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data.
I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...
This is the question:
Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:
printStrings("abc", 5);
will print the following output: abcabcabcabcabc
This is what I attempted:
public class printStringsproject { public static void printStrings(int abc, int number) { for (int i = 1; i <= number; i++) { System.out.print("abc"); } } public static void main(String[] args) { printStrings(1, 5); } }
I'm trying to write a method that will print a string of a random number of X's (from 5 to 20) on a line, then another number of X's on the next line, etc. until the random number = 16.
I have to use while loops and probably fencepost algorithms. I'm confused with how to print the number of x's the random number of times over and over.
Right now, I'm pretty sure my for loop is wrong or unnecessary.
So far, this is my code:
public static void randomX() { int x = 0; while (x != 16) { x = (int)(Math.random()*20) + 5; for (int i = 0; i <= x; i++); { System.out.print("x"); } } }
I am trying to make a program to multiplies two numbers without using the "*" operator. My idea was to add x number y amount of times, which is the same as multiplication. I did this with a for loop, but zero is always returned as the answer. How can I fix this?
public class secondMult { public static void main(String[] args){ System.out.println(multWithSum(4,5)); } public static int multWithSum(int x, int y){ int sum = 0; for(int i = 0; i==y;i++){ sum = sum + x; } return sum; } }
How do I record the number of times all runs of heads occur. Ex.
Length Number of runs of heads 1 --------------------3 2 --------------------2 3 --------------------0 4 --------------------1
public static void main(String[] args) { int [] coinToss = new int[TOSSES]; int longestRun = 0; int run = 0; for(int i = 0; i < coinToss.length; i++) coinToss[i] = toss();
I am practicing some basic recursion and I was trying to solve this problem
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words:
sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n
For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should throw an IllegalArgumentException if passed a value less than 0.
This is my attempt to do it , however my output is always a 0.0 , and i do not understand why :
public static double sumTo(int n ){ if(n<0){ throw new IllegalArgumentException(); } else if (n==0){ return 0.0;