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'};
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.
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.
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
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
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 :
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?
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
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');
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.
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.
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
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.
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;
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);
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?
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....
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.
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.
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");
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
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.
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(){
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?