How To Find How Many Times A Number Appears In Array

Dec 7, 2014

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

[Code] ....

View Replies


ADVERTISEMENT

Count And Display Number Of Times Specified Character Appears In String

Mar 7, 2014

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();
}
}

View Replies View Related

Counting How Many Times A Value Appears In Array

Nov 23, 2014

I have this code but I can't seem to get it to work. It keeps saying that "count" cat be found and that it cannot return a value whose type is void.

Java Code: public class Cuantos {
static int getPosition(double listOfValues[], double targetValue ) {
int i,count,
position = -1;
for (i=0; i < listOfValues.length; i++) {
if (listOfValues[i] == targetValue)

[code]....

View Replies View Related

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++) {

[Code]...

View Replies View Related

Create 2D Array Out Of CSV File And Find Number Of Elements To Determine Array Size

Mar 24, 2015

I am taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off. The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.

I am still stuck in the first part where the CSV has to be imported into the Array. My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.

The list consists of the following wifi related values:

MAC-Adress, SSID, Timestamp, Signalstrength.

These are on the list, separated by comma. The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.

The catch is, we are not allowed to use any of the following:

java.util.ArrayList
java.util.Arrays
and any class out of java.util.Collection.

So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.

Below is my Code (Its still an active construction zone):

public class WhatsThere {
public WhatsThere(String wifiscan) throws IOException {
}
public static void main(String[] args) throws IOException {
// WhatsThere Liste = new WhatsThere(String wifiscan);
String[][] arrayListe = new String[0][0];

[Code] ....

View Replies View Related

Find Slot Number In Array?

Jan 30, 2015

what I needed to know to complete the lesson, but now it just tells me what the output should be and I have to research the information on my own. anyways, here is my code from the previous lesson:

import java.util.Random;
import java.util.Scanner;
public class tnArray {
public static void main(String[] args) {
int [] array;

[code]....

Now I have to change it so that instead of counting the number of times a number will appear in the array, I must output which slot in the array the number is in.

View Replies View Related

Find Biggest Number From Array

Jun 26, 2014

I have 2 arrays in random order of 10 numbers.I need to find the biggest number from array A and B and then when its on a screen second thing is to multiply those numbers by 2.

import java.util.Random;
public class Projektas {
public static void main(String arng[]){
int i,j;
int A[] = new int [10];
int B[] = new int [10];;

[code]..

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

How To Find The Best Time Of 14 Run Times

Oct 19, 2014

I have this code that prints the run time for each for loop iteration but how do I find the fastest time store it in a variable and the print that variable at the end? This is what I have currently.

for (int i = 1; i <= 14; i++) {
int n = (int) Math.pow(2, i);
int[] list = new int[n];
for (int j = 0; j < list.length; j++) {
list[j] = (int) (Math.random() * 2000);
}
int length = list.length;
double radixTime = radixSort(list, length);
System.out.println("For base " + i + " the time was " + radixTime);
}

View Replies View Related

Accept String And Number Of Repetitions As Parameters And Print String Given Number Of Times

Oct 3, 2014

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);
}
}

View Replies View Related

How To Print A Character Random Number Of Times

Oct 26, 2011

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");
}
}
}

View Replies View Related

Multiply Two Numbers - Add X Number Y Amount Of Times

Jun 9, 2014

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

View Replies View Related

Recording Number Of Times All Runs Of Heads Occur

Jan 4, 2015

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();

[Code] .....

View Replies View Related

Coin Classes - Count Number Of Times Head And Tail Appear

Mar 26, 2014

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

View Replies View Related

Accept List Of Integers As Parameter And Return Number Of Times Most Frequently Occurring Integer

Nov 20, 2014

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.

If the list is empty, return 0.

View Replies View Related

JPanel Appears To Be Null

Feb 23, 2014

I've a JPanel declared as a field in a JFrame:

public static javax.swing.JPanel pane;
I initialize it in a method (Well, Netbeans' GUI builder does. :P)
pane = new javax.swing.JPanel(){
@Override
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.drawArc(8, 6, 15, 15, 0, 90); /* This works. The panel and frame are displayed, the arc is drawn, &c. */
}
};

it works and all; the arc is drawn.I want to draw on it dynamically by instantiating its Graphics (I call the "public Graphics getGraphics()" method.). This has worked for me before, taking a JPanel's Graphics and drawing with it through a different method than public void paint(Graphics g); But when I do, it comes up with a NullPointerException. WAIT! Don't go rambling on about initializing the variable because I've gotten past that NPE newbie's blockade.public static void render(Something s) { /*This is in a different file altogether. pane is public, static so I can access it from here. That's not the problem.*/

JPanel jp = Frame.pane;
Graphics g = jp.getGraphics();
g.fillRect(s.x, s.y, 4, 4);
}

Upon further investigation:
Exception in thread "main" java.lang.NullPointerException
at physics.Renderer.render(Renderer.java:30) <-- This line is "JPanel jp = Frame.pane;"
at physics.Renderer.renderall(Renderer.java:24) <--This line calls public static void render(Something s)
at physics.Updater.update(Updater.java:47) <--Renderer is just a tool used by the Updater. Sounds like a game loop, right?
at physics.Physics.main(Physics.java:31) <-- The game loop.

it says that Line 30, making jp and pointing it to pane itself is the problem.

Conclusion:
-The JPanel in the JFrame is created, initialized and overrides a parent method.
-The JPanel is not initialized, meaning that it is null. There is obviously a problem.

View Replies View Related

Drawing Image On To Screen - Nothing Appears?

Feb 7, 2014

I am trying to draw an image unto the screen, however, nothing appears...

Java Code:

private ImageIcon i = new ImageIcon("image.png");
private Image image;
...
image = i.getImage();
...
g.drawImage(image, (int)(x - r), (int)(y - r), null); mh_sh_highlight_all('java');

Am I doing it right? The program gives me no error, it's just that when I try to draw it, I see nothing.

View Replies View Related

Find The Lowest Number?

Apr 12, 2014

I have to find the average, highest, and lowest numbers in an array. I already have the average and highest, and I thought I could find the lowest number the same way I found the highest, but it's not working. It just keeps coming out to 0. I've tried out different ways, but I want to see if there are better ways than doing MAX_VALUE for the lowest, then looping it through.

import java.util.Scanner; 
public class Test_Scores {
public static void main(String[] args) {
//array,scanner,values

[Code].....

View Replies View Related

Find Number And Sum Of All Integers

Oct 29, 2014

Write a program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7.

View Replies View Related

How To Find Whether A Number Is Palindrome Or Not

Aug 6, 2014

Any simplest code to check whether a number is palindrome or not?

View Replies View Related

Find Day Number Of The Year?

Apr 15, 2014

figuring out the logic to print the day number of the year. The directions for my homework are: Write a program that prints the day number of the year, given the date is in the form: month day year. For example, if the input is 1 1 05, the day number is 1; if the input is 12 25 05, the day number is 359. The program should check for a leap year . I have a basic outline of the program but i am having trouble with figuring out the logic to display the day number of the year given the date. This is what i have so far

import javax.swing.JOptionPane;
 public class Practice4 {
public static void main(String[] args){
String monthStr;
String dayStr;
int inputMonth;

[code]....

View Replies View Related

Find Number Of Rows In CSV File

Feb 13, 2014

I am working through a text and I am supposed to complete the following tasks.

Your ReadFiles.java class requires the following methods:

Method: check to see if the file exists

Method: find number of rows in csv file

Method: Converts the csv file to a mutli-dimensional array

Method: PrintArray

Method: Return array using a get method

Create a file DataAnalyzer.java. This file will be used to call the methods in ReadFiles.java. Be sure to demonstrate that all of your methods work through DataAnalyzer.java.

The problem is that it does not really provide any information on how to go about reading a file into an array. I am stuck at the third task of converting the file to an array and I have tried several ways to do this unsuccessfully. I thought that I would at least try to get things to print out (line 87) to see if I could get that to work, but all that prints in null over and over again.

Java Code:

public class DataAnalyzer {
public static void main (String[] args) {
ReadFiles aReadFiles = new ReadFiles();
aReadFiles.fileCheck();
aReadFiles.findRows();
aReadFiles.convertFile();

[Code] .....

View Replies View Related

Java Math - Find Imaginary Number

Nov 2, 2014

I am doing this math for my assignment where you have to find the imaginary number.

double dem = (b*b-4*a*c)*-1;
double dem2 = Math.sqrt(dem);
realPart = (-b/(2*a));
imagine = (dem2)/(2*a);
imagineneg = (dem2)/(2*a);

So basically I am trying to get it that the number inside the square root isnt negative. So lets say I enter a: 1, b: -1, c: 4.25. When you do the discriminant it would be -16 but I want that to be positive so I multiple it by -1 (thats the dem part). Then I sqaure root the whatever the answer is and then go to the imagine and imagineneg part where you find the imaginary number.

My problem is that instead of it being -16*-1 to get 16, some how I get 15.

View Replies View Related

Find If Number Entered By User Consecutive Or Not

Feb 21, 2014

I need to find if the number entered by the user are consecutive or not!

Ex:this should return a true!! This should return false!

(1,2,3) (3,5,7)
(3,2,4) (1,2,2)
(-10,-8,-9) (7,7,9)

My program seems to work ok when i enter number in order like 1,2,3 = true , and all the numbers for false seem to be working as well! my problem is when i enter number like 3,2,4 that are not in order but still are consecutive!! I thought that another if statement would be the solution but i have tray several different ones and still can't make it work !!!

Java Code:

import java.util.*;
public class Consecutive{
public static void main (String [] args){
Scanner console= new Scanner(System.in);
System.out.println("Enter three numbers");
String numbers = console.nextLine();

[[Code] .....

View Replies View Related

Find ASCII Number Of Names Entered

Sep 12, 2014

I am having a problem with SD array .i have to find the ASCII no. of names entered.

import java .io.*;
class ASCII
{
public static void main()throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);

[Code] .....

View Replies View Related

Find Number Of And Sum Of All Integers Greater Than 100 And Less Than 200 Divisible By 7

Oct 30, 2014

 Write a program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7.

View Replies View Related







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