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


ADVERTISEMENT

Generating Random String Of Integers - No Repeating Numbers In Java

Nov 9, 2014

I don't want to use an array. I do not know of a function built into Java to allow me to do this.

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

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

Unable To Print Out Randomly Generated Array

Nov 19, 2014

i am trying to print out a randomly generated array, but i only get

[I@7852e922

i did some research and the "[" is for arrays, "I" is for the int and "@" is the hash. and the rest is some sort of hex. I need to override this in a way, but i can't seem to find out how.
this is my current code:

import java.util.Random;

public class Oppgave {
public static void main(String[] args){
int myint[] = fyll();
System.out.println(myint);
}
public static int[] fyll() {

[Code]...

View Replies View Related

Unable To Print A Loop Inside Array Of Greater Size Than Loop Itself

Jan 27, 2015

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.

int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;

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.

View Replies View Related

Unable To Sum All Integers In Binary File

Dec 4, 2014

iam trying to sum all the integers in a binary file. the integers are 0-9. IAM having trouble exiting the while loop to display the sum. below is what i have so far which is not displaying the sum.

import java.io.*;
public class binaryAdd{
public static void main(String []args)throws IOException{
DataOutputStream output= new DataOutputStream(new FileOutputStream("myBinary.dat"));
for(int i=0;i<10;i++){

[Code]...

View Replies View Related

Print Range Of Integers From X To Y With Increment Of 5

May 6, 2015

Working on problem in my book in which I have to print a range of integers from x to y with an increment of 5. I thought I had the right idea when writing out this code, but apparently, it only gives a few of the numbers in the range, not all, what I am doing wrong?

import java.util.Scanner;
public class Ranges
{
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter a value for x and y: ");
int x = input.nextInt();

[Code]...

import java.util.Scanner;
public class Ranges
{
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter a value for x and y: ");
int x = input.nextInt();

[Code]...

View Replies View Related

Print Maximum And Minimum Values Of Integers From User Input

Apr 27, 2015

So I'm learning java from a website and I was tasked with creating a simple program which allows the user to enter a series of integers, then finally when they decide to input a non-integer the program will print the maximum and minimum values of the integers they entered. So for example if they entered 5, 4, 3 and 2 then enter a non-integer the program would output 5 (maximum value), then 2 on a new line (minimum value).

Here is my code:

import java.util.Scanner;
public class MaxMinPrinter {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

[Code] ....

And this is what the output looks like:

Actual output
-------------------------------------------
Enter an integer: 5
- 10
-
- Enter an integer: -4
- 8
- -6
-
- Enter an integer: 11
- -1
-
- Enter an integer: q
- -1
- -6

When it's supposed to look like this:

Expected output
-------------------------------------------
Enter an integer: 5
Enter an integer: 10
Enter an integer: -4
Enter an integer: 8
Enter an integer: -6
Enter an integer: 11
Enter an integer: -1
Enter an integer: q
11
-6

View Replies View Related

Separating Integers Into Individual Digits And Print Separately By Three Spaces Each

Mar 11, 2014

Write an application that inputs one number consisting of five digits from the user, separates the number into in individual digits and prints the digits separated from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each.

For example, if the user types in the number 42339,

the program should input 4 2 3 3 9.

Here is my code so far but I am stuck.

mport java.util.Scanner;
public class Seperating {
public static void main ( String[] args) {
Scanner input = new Scanner(System.in);
int number = 0 ;
System.out.printf("%d" , number );
System.out.print("Enter integer");
number = input.nextInt();
}
}

but I am not getting the result I wanted what am I doing wrong

View Replies View Related

Unable To Print Sum Of First Row?

Dec 11, 2014

Java Code:

public class Lab12 {
public static int SumRow(int [][] a){
int row, column, sum=0;
for (row=0;row<2;row++ ) {
sum=0;
for (column=0;column<2;column++) {
sum=sum+a[row][column];

[Code]...

my output is

The sum of row 1 is: 3
The sum of coloumn 0 is: 5
The sum of coloumn 1 is: 5
The max of row 0 is: 4
The max of row 1 is: 2

it is supposed to print the sum of row o first ?

View Replies View Related

Unable To Print Values As Specified

Feb 8, 2015

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 1000.

public class ClassEcerciseOne {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x=0;
int y=0;
for (x=0;x < MAX_VALUE;x++){
if (x/3==0){
System.out.println(x);
int z= x++;

[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

I/O / Streams :: Unable To Create A Print Writer To Write To File

Apr 25, 2014

I am completing a USACO online problem and am trying to create a print writer to write to my file(ride.out). I did this:

PrintWriter out = new PrintWriter(new BufferedReader(new FileWriter("ride.out")));

However, a load of undefined constructor errors come up for PrintWriter(BufferedWriter) and BufferedWriter(FileWriter). I have imported java.io.* so I don't know what the issue is. This has worked before.

edit: bufferedreader? i give up(not literally)

View Replies View Related

Unable To Print Out Results Of Program That Calculate Number Of Seats

Nov 11, 2014

I'm trying to print out the results of a program that calculates the number of seats the parties will get in an election.I have to print the partial results and the national results.

I can print te number of seats per party in each constituency, but how can i sum all seats per party in each constituency and print the national results?I'm working with vectors, which I know it might not be the best option, but everything is working, except the fact that I can't loop throuhg the vector and retrieve the total sum per party.Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.Is it possible, or doIhave to create a method in Parties class to solve the problem?

This is what I have now.

for (Parties p : h.geral) {
show += String.format("Constituency - %5s - %5s - %d%n",
p.getConstituency(), p.getParty(), p.getNum_seats());
}

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

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

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

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

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

Repeating Constructors With Same And Different Parameters

May 26, 2014

So while experimenting with constructors, repeating constructors with the same parameters, and with different parameters. I got an output -explaining how I got it.

I made 2 classes. a "support class" (has all the info) and an "execute class" (executes info).

Support:

package inschool;
public class Constructors {
String video;
public Constructors() {
video = "frozen";

[Code] ....

Execute:

package inschool;
//this is part of Constructors class
public class App{
public static void main(String[] args){

[Code] ....

The Output:

the video name is frozen
Second constructor running
Constructor running!
the video name is frozen

Output Explanation:

constructor call number 1 and 3 are the same (essentially) and both refer to the same constructor.

My Question: if both call #1 and #3 refer to the same constructor, why is the output for #1 "the video name is frozen"

while the output for #3 used both methods in the accessed constructor-with the resulting output as
"Constructor running!"
and
"the video name is frozen"

I double checked the output-and this time made sure to scroll up ... its the same result

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







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