Printing Random Values From Array List
Jun 15, 2014
I am having a hard time trying to figure out how to print random numbers from a an array list. I tried google but nothing worked. I have to pick certain values from two lists and print them on the screen. I have included comments in the code to facilitate the explanation.
import java.util.Random;
public class Parachute {
public static void main(String[] args) {
Random randomNumbers=new Random();
int number;
int array []={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
char A[] = {'a', 'b', 'c','d','e','f','g','h', 'i','j','k','l','m','n','o','p','q'};
[Code]...
View Replies
ADVERTISEMENT
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
Feb 16, 2015
filling out a Random array: An Array of Specific Length Filled with Random Numbers This time what I need to do is take the elements from this Random array and assign them to a new Byte array:
for(int i = 0; i < limit-10; i++) {
Random dice = new Random();
int randomIndex = dice.nextInt(array.length);
if (array[randomIndex] < 128) {
System.out.print(array[randomIndex] + " ");
} else if (array[randomIndex] >= 128) {
System.out.print(array[i] + " ");
}
}
byte[] noteValues = new byte[]
{ 64, 69, 72, 71, 64, 71, 74, 72, 76, 68, 76 }; //This is the byte array filled manually!
I've tried amending the manual input to fit in with the Random array, as follows:
byte[] noteValues = new byte[]
{ array[randomIndex] };
In this case, however, the Byte array can't interpret the int values. Also, if the Byte array is outside the 'for' loop, array[randomIndex] cannot be resolved.
View Replies
View Related
Oct 26, 2014
Write a program that creates an array that can hold 9 double values that represent baseball batting averages for a starting baseball lineup. Use a for loop to populate array with random double values in the range of 0.00 to 0.500. Recall that "double" values are what Java calls "real" numbers. Use a second for loop to print the values in the array with one number per line. Finally, use a third for loop to traverse the array and find and print the maximum batting average in the array. Note: you will need to use String.format to control the precision of a double number when you print it- Here is my code so far:
public class P2F {
public static void main (String[] args) {
double [] player= new double [9];
//player[0]= Math.random();
for (int index=0; index < player.length; index++) {
[Code] ....
When I open the terminal window I get different variations of this [D@4545c5]. I would like to know all the things I am doing wrong.
View Replies
View Related
Feb 7, 2015
I have a problem where I have to create random integers from 1 to n + 1. When the array is displayed I have to find the missing integer from the values that are displayed and show that value. I have to use O(n^2) and O(n) operations. I have created this code so far but I cannot figure out how to make my values display n + 1. It is only displaying values 1, 2, 3, 4 and 5 in random order. I also cannot figure out how to find the missing value. I have created a boolean displayed statement but can't determine how to use it in this code.
=Java
import java.util.Random;
public class Task6
{
public static void main(String[] args)
{
int[] numbers = new int[7]; //create array of numbers
Random random = new Random();
boolean displayed = false; //boolean value to determine if number was displayed
[code]....
View Replies
View Related
Jul 8, 2014
I am working on this project that wants me to write a program that inputs 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, display it only if it is not a duplicate of a number already read. The only part I am confused about is how to go about checking for duplicate values that the user may enter. And IF the user does input a duplicate value, it should not be stored again.In addition, the value entered should be printed out after it is entered along side the value that have been previously entered by the user such as:
23
23 45
23 45 67
23 45 67 12
and so on.
I am still fairly new at java programming.
import java.util.*;
public class NumberArray
{
public static void main(String[] args) {
// declare an array with 5 elements
[code]....
View Replies
View Related
Jan 30, 2015
Here is the java code that i compiled on eclipse but each time i run it i am getting different sequence of output !!
What I did is that my main() calls a function m1() which calls a function m2() which throws an exception back to m1() which throws exception back to main().
public class ClassB {
public static void main(String[] args) {
try {
m1();
} catch (Exception ex) {
ex.printStackTrace();
[Code] ....
The output is as shown in screenshots at different times :
SCREENSHOT 1:
SCREENSHOT 2:
SCREENSHOT 3:
How is this possible ??
View Replies
View Related
Feb 20, 2015
I'm trying to print sentences from a list which read in sentences from a text file. I'm able to get the program to print out the sentences in a numbered format corresponding to each sentence, but the first sentence in the list is always repeated, same as the next sentence. How can I print out the contents of a list in a numbered format (1. _ 2. _ 3._) without repeating sentences?
Here's my code:
package jacsim;
import java.util.Scanner;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.io.FileInputStream;
[Code] ....
The output looks like:
Input Sentences:
1:The cat in the hat
2:The cat in the hat The cat sat on the mat
3:The cat in the hat The cat sat on the mat Pigs in a blanket
Sorted Single Arrays:
I'm trying to make it look like:
1. The cat in the hat
2. The cat sat on the mat
3. Pigs in a blanket
View Replies
View Related
Sep 21, 2014
Right, so I got this method that creates and sorts 2 lists. What I want to do is merge these lists into a third list, have it sorted, and then print the contents of the list. The problem is, I'm tired and I don't remember how I can print it.
import java.util.*;
public class MergeTwoSortedListWilson {
public void CHANGEME() {
// To do
ArrayList<String> aList = new ArrayList<String>();
aList.add("Banana");
[Code] ...
MergeTwoSortedListWilson.java:35: error: cannot find symbol
System.out.println(aList);
^
symbol: variable aList
location: class MergeTwoSortedListWilson
1 error
View Replies
View Related
Feb 17, 2015
I'm trying to print the contents of my linked list. I'm using nodes and within those nodes it hold String data. So i want to print out the data within the nodes. Whenever i do
System.out.println(node1.data),
it prints perfectly. But i'm trying to use a method where it would loop through the list and print out the data for every node in it. when i run my print method i get results such as
Node@15db9742
Node@6d06d69c
Node@7852e922.
Here is my print method i created
Java Code:
public void print(){
Node<E> current = head;
while (current.next != null){
System.out.println(current.data);
current = current.next;
}
} mh_sh_highlight_all('java');
View Replies
View Related
Apr 16, 2015
I have my code which will generate a random string using an ArrayList. However, I aso want to have it generate them in random lengths, not just using all the chars and numbers.
public static void main(String[] args) {
String[] CHARS = {"A","B","C","D","E","F","G",
"H","I","J","K","L","M","N",
"O","P","Q","R","S","T","U",
"V","W","X","Y","Z","1","2",
"3","4","5","6","7","8","9","0"};
List<String> pass = Arrays.asList(CHARS);
Collections.shuffle(pass);
for (String chars : pass) {
System.out.print(chars);
}
}
View Replies
View Related
Feb 5, 2014
A "postings list" has a data field, a field for the next pointer and a jump field - the jump field points to any other node. The last node in the postings list has next set to null; all other nodes have non-null next and jump fields.
Implement a function that a postings list as input and returns a copy of the list, in O(n) time and O(1) storage beyond that required for the nodes in the copy. You may modify the original list but must return it to its original state before returning.
I was able to find a solution, but not one that runs in O(n) time.
View Replies
View Related
Nov 3, 2014
Everything is written up and looks good and i get no compile errors but every student object other than the default constructor has null and zero values when printed.
import java.text.DecimalFormat;
import java.lang.Math;
public class Student
{
//instantiate variables
[Code] .....
View Replies
View Related
May 4, 2014
Write a subclass of BasicGame in which the computer asks the user to choose a whole number from 1 to 100, inclusive. The program then makes a number of guesses. After each guess, the user tells whether the guess is too high, too low, or exactly right. Once the computer "knows" what the correct number is, the computer tells the user the correct number and the number of guesses tried. Have the program ask the user after each game whether she wants to play again. Design the program so that it always guesses the correct answer by the seventh try at latest. Hint: Have two instance variables that keep track of the smallest and largest values that the guess could be (initially 1 and 100). After each wrong guess, update these smallest and largest values appropriately.This is what I have written for the assignment:
Java Code:
import javax.swing.JOptionPane;
public class GuessThatNumber extends BasicGame
{
private java.util.Random randy;
private int itsCompNumber;
private int itsHigh = 99;
private int itsLow = 1;
[code]...
The part I'm having trouble with is changing the high and low values based on the input of "too high" or "too low," which I tried in the continueGame method... When I test the game, the values for itsCompNumber aren't restricted by the user's input at all, and I'm not sure how to fix it.
View Replies
View Related
Sep 30, 2014
Its a program that creates a singly linked list that stores names and high scores and prints them. For some reason it is printing an entry extra times. Also my remove function is not working properly.
package project;
public class GameEntry implements Comparable<GameEntry> {
private String name;
private int score;
public GameEntry(String n, int s) {
name = n;
score = s;
[code]...
View Replies
View Related
Dec 14, 2014
For part of my program, I am trying to ask the user for input on generating a number of random characters. I have already done this with integers and doubles, how would I do this for characters using ASCII values? Would I use the same format as I did for generating integers (Shown in code)?
import java.util.Random;
import java.util.Scanner;
public class NewNumberCharacter {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
[code]....
View Replies
View Related
May 1, 2014
I have a random list of links whose title I am getting from the database. I'd like to catch one of these links when the user clicks one of them and process it from a single jsp file. Possible?
<ul>
<%
Random random=new Random();
TreeSet<Integer> ts=new TreeSet<Integer>();
for(int i=0;i<8;i++) {
randomInteger=al.get(random.nextInt(al.size()));
ts.add(randomInteger);
[Code] ....
E.g.: when user clicks one of these links, can I dynamically call a web page that fetches information from the click and gets values from a database and insert it on JSP page....
View Replies
View Related
Apr 30, 2015
I am trying to make a method that generated a random number between two values that can be negative or positive.
So:
rand(-0.2, 0.2);
would give one of these: -0.2, -0.1, 0, 0.1, 0.2
View Replies
View Related
Nov 3, 2014
The only problem I have now is getting a method to return the median element of a LinkedList without using loops of any kind or by using a global counter anywhere.
I've pretty easily figured out how to get the index value for the median number (there is some lee way allowed. If the list has an even size, any of the middle values are accepted) but I can't figure out how to print it without loops.
I'm sure I need to make a method that finds an element at the given index value, but I don't know how to do it without loops.
Here's all of my code. Inside is my Assignment3 class I use for testing, StudentList which contains the LinkedList head and other List methods, and StudentNode which is obviously, the Node class. Also I've attached the first test1.txt file as well.
import java.io.FileNotFoundException;
import java.util.*;
public class Assignment3 {
public static void main (String []args){
StudentList<StudentNode> myList = new StudentList<StudentNode>();
[Code] .....
I tried making a method that basically counts up the list recursively then a second method that counts down recursively and is supposed to stop once it hits the middle number, then print that node.
Attached File(s) : test1.txt (106bytes)
View Replies
View Related
Apr 20, 2014
Currently, my program converts Long values to String. And when I test it out, it do print out the correct output. However, when the converted String value is passed over to be written in a text file, it seems that BufferedWriter isn't printing out the outcome that it's supposed to be.
saltVs = Long.toString(saltV);
System.out.print(saltVs); //will print out 79723172
Now the problem is here...It only prints out the last digit of the String value (instead of 79723172).
Here is my FileWriter/BufferedWriter part.
Why is that when I run my program using command prompt, it prints out the output that I wanted, but however when it comes to writing to the file, it doesn't come out right.
View Replies
View Related
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
Apr 15, 2013
I am trying to create a method in BlueJ which needs the values from a Map to be copied into a List so I can use the List methods min() and max(). Here is the method I have created so far:
public String findNamesInPageRange()
{
int minOfRange = Integer.parseInt(OUDialog.request("Input number for start of range"));
int maxOfRange = Integer.parseInt(OUDialog.request("Input number for end of range"));
String foundIt = null;
for(String eachKey : bookIndex.keySet()) {
List<Integer> pageList = new ArrayList<>(); //Cannot access values from a map using this List
if((minOfRange >= Collections.min(pageList)) && (maxOfRange <= Collections.max(pageList))) {
foundIt = eachKey;
}
}
return foundIt;
}
As can be seen there is a comment showing where my problem lies.
The Map is coded as Map<String, Set<Integer>> bookIndex = new TreeMap<>();
Ive tried using bookIndex.values() as the argument of the List but I get an incompatible type error
So, how can this problem I am having be resolved?
View Replies
View Related
Jan 22, 2015
first i looked at this example and understand this fine:
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
[Code]....
View Replies
View Related
Mar 19, 2014
a simple Java program for finding the median value in a list of values with the following requirements:
- Create an array with an even number of values in it (an odd number of values is little bit trickier, so if you want a challenge, do it for either an even or odd number of values)
- Find the value with an equal number of values greater than the value as there are values less than the value
- Your solution must not require a sorted list of values - Output the median value
This assignment is intended to get you to demonstrate basic knowledge of arrays, and to create methods with both input and output.
View Replies
View Related
May 2, 2014
So I'm trying to write a program that will take in 5 numbers, store them in an array and then print out the array and the average of the array. Problem is that every time I run my program I get a "ArrayIndexOutOfBoundsException: 5" error and I don't know who to fix it.
public class ConstructorHomework {
final static int size = 5;
static double[] myArray = new double[size];
public static double average;
public void printArray(){
[code]...
View Replies
View Related
Nov 3, 2014
I have a fixed list of 20+ values to be populated in a dropdown. The set of values are always going to remain the same, no future scope of modifications, additions or deletions.
Listing each of them as <option> in JSP is making the page look cluttered. Or is it better reading it as a comma-separated string from a properties file and then iterating to populate the dropdown.
What will be the best approach to populate such a dropdown in JSP?
View Replies
View Related