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


ADVERTISEMENT

Randomly Generated Labyrinths

Dec 13, 2014

So i am making this game with randomly generated labyrinths and i get error in array. Here is the code:

import java.util.Random;
public class Labyrinth {
 
//0-walls
//1-path
 
[code]....
 
I've got lost in some things so my code might contain unnecessary code.

View Replies View Related

Passing Parameters - Randomly Generated Numbers Not Appearing

Dec 14, 2014

For some reason, when I test out my code, my randomly generated numbers don't appear. Here is a sample result:

> What do you want to generate, integer, double, or character?

>integer

>What is the upper limit and lower limit of the integers you want to generate?

>1

>10

>How many integers do you want to generate?

>10

>BUILD SUCCESSFUL (total time: 9 seconds)

Is this because my code is not passing my parameters correctly? I'm not sure how to fix this either.

Here is my code for reference (it's not completed at the moment)

import java.util.Scanner;
public class NewNumberCharacter {
/** Main method
* @param args */
public static void main(String[] args) {
int return_int;
double return_double;

[Code] ....

View Replies View Related

Guessing Game GUI - Comparing Guess With Randomly Generated Number

Apr 13, 2014

package guess.the.numbers;
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.*;
ublic class GuessTheNumbers extends JFrame{
private JButton guessBtn;
private JButton restartBtn;

[Code] ....

I am getting a strange error and it almost seems like its not comparing it to the random generated number just the guess that i entered before. Here are my error messages.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at guess.the.numbers.GuessTheNumbers$ButtonHandler.actionPerformed(GuessTheNumbers.java:119)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)

[Code] .....

View Replies View Related

Identity Program - Check If Randomly Generated Number Match Index

Apr 14, 2015

I have this program where I'm supposed to fill an array with 1000 indices with 1000 randomly generated numbers between 1 and 1000. The program is supposed to check if any of the numbers match an index that is the same value (so for example, the number 4 is in index 4). How to check for that condition, especially using a binary search (I'm also told to use a binary search).

Right now the variable index isn't initialized because I don't know what to initialize it to exactly. How do I check to see if any numbers match the value of the same index?

import java.util.*;
public class Identity {
public static void main(String[] args) {
int [] integers = new int [1000];
// Fill array with randomly generated numbers
int [] display = GenerateRandom(integers);

[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

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

Random Array - Only 2 Of 10 Possible Numbers Are Generated

Nov 21, 2014

I am writing a program that creates an array with random numbers. Then the user can choose what number of the array he/she wants to check the occurance of. This works fine, but the numbers generated seems very weird, only 2 of the 10 possible numbers are generated. 0 and one of the other 9 numbers, the 0 is always 10100 and the other one is always 101.

import java.util.Random;
import java.util.Scanner;
public class OppgaveC {
public static void sjekk() {
int randomArray[]=new int[101];
int countArray[]=new int[10];
Random rand = new Random();

[Code] ....

it also says i have a memory leak on my scanner, ow can i close this?

View Replies View Related

Randomly Get Index Of Array

Jan 7, 2015

I created and an Array of integers how can I get randomly get/pick the index of each array element.

View Replies View Related

Randomly Fill 2D Array In Constructor

Dec 7, 2014

I must fill a 2d array randomly and then apply methods to the array. However i keep getting an out of bounds exception no matter what dimensions i use. I have a test and a class program.

import java.util.*;
import java.lang.Math;
import java.util.Arrays;
import java.util.Random;
class SummerStats {
Random rand = new Random();

[Code] .....

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

How To Access 4 Strings Randomly From String Array

Jun 13, 2014

Now I am trying to print the 4 string randomly from string array..where string contains no of words which are splitted from the file....

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

How To Randomly Assign Specific Values In Array Or ArrayList

Apr 3, 2015

I'm trying to build a monopoly like game, and atm I'm trying to find way how to build the Community and Chance chest cards. so far, my logic is

1-create an ArrayList of cards with a given order

2-for a given number of times(for loop) generate 2 random numbers ,which will be the parameters for Collection.swap().

3-swap.

here's the code for the shuffler Button

shuffler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<shuffeledMessages.length;i++){
int randoma=(int)(Math.random()*4);
int randomb=(int)(Math.random()*4);
Collections.swap(myMessages,randoma,randomb);
}
}
});

For now things seem to work pretty ok, but I'm wondering if this is a good and efficient way to shuffle a card chest especially in case of large number of cards. plus, I'm not sure what would be a good loop count for effective shuffling, in my case I used i<arraylist.size

View Replies View Related

How To Randomly Select A String In Array Based On Corresponding Number

Dec 21, 2014

I'm trying to generate a random word from the array that I have made including the words by making it corresponding with a randomly generated number. So for example, if the number generated is 0, then the word that the person has to guess would be "AUNT". How would I transfer the randomly generated number from one method into the array method to get the word the person has to guess?

Write a program called Word Guessing Game. Open the file FourLetterWords.txt and write the contents into an array of Strings (the file has 87 words in it). Then use a randomly generated number between 0 and 86 to select a word. The player will then try to guess the word selected by the game. The player is allowed 7 tries, if the player does not guess the word on the 7th try he/she losses. Display the letter of the word as they are guessed in the correct order, you will also display the incorrect letters. The game is over when:

- The player completes the word, or guesses the whole word correctly.
- The player does not guess the word in seven tries.
The player must also be allowed to terminate the game.
The game must have at least 5 classes:
- Main Class
- Class to return a random integer between 0 and 86.
- Class to return a populated array of 87, 4 letter words.
- Class to return a character that the player enters from the keyboard.
- Class to display both the correctly guessed letters and the incorrect letters.

My code (it is not complete, my attempt to do what I am trying to do is obviously not working.)

import java.util.Scanner;
public class WordGuessingGame {
public static void main(String[] args) {
final int NUMBER_OF_WORDS = 87;
RandomWordGenerator.random(NUMBER_OF_WORDS);

[code]...

View Replies View Related

Create For Loop That Randomly Assign Values To Each Element Within Array?

Apr 16, 2014

Started learning about Array's I'm doing an exercise where you create a for loop that randomly assigns values to each element within the array, but where is my code going wrong?

import java.util.Scanner;
public class ArrayExamples{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double exampleArray[] = new double[5];
System.out.print("Enter a Number: ");
int num1 = input.nextInt();

[Code] .....

View Replies View Related

Placing Randomly Chosen Words From Text File Into 2D Array

Apr 5, 2014

I have an assignment for college that involves placing randomly chosen words from a text file into a 2-d array. I have nearly completed ithowever I am having difficulty with a list of string type.What I have done so far is below,

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.lang.Math;
 
[code]....

when I try to run an instance of the program(using BlueJ) I get an error saying there is a null pointer exception at line 35 which is wordsForGrid. add (puzzleWords.get(pos));

I am thinking that this is related to the fact that at this line in the loadWordsFromFile method List <String>words=new ArrayList<String>(); I could not create an empty list of string type, i.e List<String>words = new List<String>();

Is this correct? Or am I looking in the wrong place. The textfile I am using contains over 6000 words on a separate line for each if that makes any difference.

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

Convert 2D Array To String Using ToString To Print Array

Apr 19, 2015

Trying to convert 2D array to String using toString() to be able to print the array but when I try to use it I just get the memory location

public class Forest
{
private int h;
private int w;
private double p = 0.7;
private int[][] f;
Forest(int w, int h)

[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

New Method Array To String Or Just Print As Array?

Feb 20, 2014

I just tried to fill an array with some numbers, calculated by a other function.I just tried to print this array as array, but it doesnt work. Maybe its just about the main method.

public static void main(String[] args) {
ggT(5);
}
 
public static int ggT(int a, int b) {
 
while(a!=b){
if(a>b) {
a=a-b;
} else {
b=b-a;
}
}
return a;
 
[code]....

View Replies View Related

Unable To Pass A Two Dimensional Array To A Method

Sep 27, 2014

I have a question about an error I am getting when trying to pass a two dimensional array to a method. I keep getting the "incompatible types, int cannot be converted to int[][]". I am getting the error in a few different place (see comments - at the first call of the method, at a recursive call, and at the return statement. I believe I am passing the same type of array in all cases to the type of array defined in the method parameters.

Below is my code.

// this is a call from the main method
int[][] c = new int[temp1.length][temp1.length];
c = MatrixMultiply(a,b); // this is first place the error occurs
} // end main
public static int MatrixMultiply(int[][] A, int[][] B) {
// throw new UnsupportedOperationException("Not supported yet.");
int a[][] = A;

[Code]....

View Replies View Related

Unable To Return Array And It Is Printing Out Random Characters?

Dec 21, 2014

This is my code:

public class rotate
{
public static void main(String[] args)
{
int[] arrayInput = {1, 7, 8, 6, 2};
for(int i = 0; i<arrayInput.length; i++)

[Code]...

It's printing out this (the second line):

View Replies View Related

Unable To Create Method That Calculates Standard Deviation Of Array

Jun 15, 2014

I am trying to create a method that calculates the standard deviation of array. What I want to be able to do is something like this

package standardDevaitionAndMean;
public class StandardDeviationTest {
public static void main (String[] args)
{
int [] array = {12,12,12,12,12,12,12};
standardDev = array.StandardDevation();
System.out.print(standardDev);
}
}

With something like this

package standardDevaitionAndMean;
public class StandardDeviation
{double mean1;
double standDev;
public double Mean(double ... array)
{
for(int i=0; i<array.length;i++)

[Code]...

So basically I want to be able to make an array in a class and be able to calculate its standard deviation with my method in my other class. I know the code I wrote is terrible but I just wanted to show what I am trying to do. I am kind of shaky on how arrays operate

View Replies View Related

Name / Zip Array Read And Print

Sep 22, 2014

Designed to store a first name (string), last name (string), and zip (int). Assume each line will contain two strings followed by integer each separated by tab. Then print.

I have my two class files (one for the individual and one for the array) and then my driver below. I think my only problem is my driver. I think I'm reading the data file wrong but not sure how exactly.

test in .txt file:

JohnSmith12345
JohnDoe12346
SueSmith09877
VeronicaVarguez67890
MuhammadMaliki54321

public class Person
{
private String firstName;
private String lastName;
private int zipCode;
public Person(String fName, String lName, int zCode)

[Code] ....

View Replies View Related







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