i'm trying to write a program that uses an array to store 10 numbers. The numbers should be randomly generated ( Math.random() ), and they should be between 1 and 100 ( 1 and 100 inclusive ). The program should produce an output like the one below:
Element 1 = 23 ( Odd )
Element 2 = 15 ( Odd )
Element 3 = 32 ( Even )
Element 4 - 10 ( Even )
Element 5 - 99 ( Odd )
Element 6 - 1 ( Odd )
[Code]...
I have written code for this but its only showing me 0's after first number can you check whats wrong with my code. my code is.
class even/odd{
public static void main(String[]args){
int y=0;
int z=0;
int[] array= new int[11];
for(int x=1; x <array.length ; x++){
array[x]= (int) (Math.random()* 100);
Code below. I am not sure if my logic is correct. I want to prompt a user to enter registration numbers from 100 to 1000, store the numbers in an array then sort them.
import java.util.Scanner; class regnumber { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("registration number ", 100,1000);
Write a Java program that asks the user to store 10 numbers in an array. You should then print the array in the order entered and then print in reverse order. However, instead of putting all the information in the main, you should use a class called PrintIt. The PrintIt class should have an instance variable that is an array to hold the numbers. You should have a method that will print the array in the order entered. You will also need a method to print in reverse oder. Then create a tester class that asks the user to enter 10 numbers and puts them in an array.
So far I've got this.
public class printIt { int i; int [] numArr = new int [10]; public printIt () { i = -1;
[Code] .....
Output:
Enter a number:
8 34
Forward:
8 34
Reverse:
34 8
end
what i want as an number is being able to print out 10 numbers but this only lets me do two numbers. Why is that so?
"Create a project called RainFall and a class nameD RainFall. Write a program that stores the total rainfall for each of 12 months into an array of doubles. The program should display total rainfall for the year, the average monthly rainfall, the month with the most rain and the month with the least rain. When outputting the month with the most and least rain, output the name of the month. This is what I have so far.
The main method will drive your program by doing the following:
-Create an array to hold all the individual golfers and par scores (type is Golfer[ ]). -Prompt the user for a data file containing the Par scores and the player names and score. The format of the input file should look like
public class ReverseOrder { public static Scanner input = new Scanner(System.in); public static void main(String[] args){ int[] numbers; double[] reverse;
<Declaration of the array for storing random integers and other necessary variables and / or constants. >
private int numbers; private int max; private int[] integer; private Random generator; public integer ( int n, int m )
[code]....
I need to create a program that draws random numbers and stores them in an array. How many numbers to be drawn is dependent on the array length, which is a parameter in the class constructor. (The entire array to be filled!) The program shall, however, just save the figures are not drawn already. (Ie, the array must contain only one instance of each numeral.) All figures drawn should be in the range of 100 to 1000, both limits included. These limits are defined as named constants. When all the numbers are generated and stored in the array, the program should find the largest, smallest and average value of the numbers in the array. In addition, it should find the value closest gjennomnstittetsverdien.
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've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.
[ public class Array1 { public static void main(String[] args) { int array [ ] = new int[100]; for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 26);
i have a program that is to store DVD information and when i enter all the information asked it returns no info. I have a DVD inventory file and a DVD extended file.
DVDinventoryprogram class package inventory2; import java.util.Scanner;
How could I store the currency and amount in an array? A 2x2 array would do this but how to store them and retrieve them is the challenge. For example, I have a method that asks the user for two inputs, the currency and the amount and using the array as a chat table where I could map the currency to the the currency input entered by the user, I could do some calculations with the amount entered by the user. how I could represent the 2x2 array?
I have a file called statecapitals.txt that is read in, I want to store it in either a 2d array or hashmap and select a random state then Ask the user for the name of the capital. Then I want to Let them know if they are correct or not and have a choice to play as many times as they like. When they no longer want to play,I want to let them know how many they got correct and how many incorrect. I am not sure which would be better a hash map or 2d array and dont know where to start with each.
here is what the text file looks like:
Alabama - Montgomery Alaska - Juneau Arizona - Phoenix Arkansas - Little Rock California - Sacramento Colorado - Denver
What is printed is much larger than what I've copy and pasted but I can't post it all here.
The error I get is:
Exception in thread "main" java.io.IOException: Stream closed at java.io.BufferedReader.ensureOpen(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at Map.importMap(Map.java:26) at Map.main(Map.java:44)
So i wanted to try something new like find an image within an image. So for my "find" method I would like to take an image and use it to scan and compare sum of absolute differences with the bigger image. So that the smallest SAD would be the exact image that I am using to scan. What I am thinking is to put each pixel value of both images into two separate arrays and compare them via Math.abs(image1[i][j]-image2[i][j]); . My only problem is that I do not know how to put each pixel value into an array.
Also, If I only want to compare just the green in the picture. I saw that the Pixel class has a getGreen(); method. If I want to find the SAD of the green, would Math.abs(image1.getGreen()-image2.getGreen()); work? I was planning to have 2 nested loops running through each column and row for each image and just find the SAD of the green value.
I want to take my 5x5 array that reads data from a file, add the rows and columns, then print out a new 6x6 array with those values. Here is my code that shows I understand everything except this.
import java.io.*; import java.util.*; public class prog470cAddingRandC { public static void main (String [] args) { //read the file Scanner inFile=null;
[Code] ....
I was told in a previous question to make the starting array 6x6 but that is not working. How do I take sum1 and sum2 and store that along with the values of the old array and print it?
Write a program that prompts the user for information about some countries, creates an object for each country, and then stores the objects in an array. After the user has entered information about all the countries, your program should print out which countries in the list have the smallest and largest area and population density. Assume the user will enter information about at least one country but that the program will not have to store more than ten countries in the array. The user will indicate that they are done entering countries by typing "DONE" for a country name. Here is an example of what your program must look like when it is executed (user input is shown bold)
Please enter the name of a country: United States Enter the area in square km and population of United States: 9827000 310000000 Please enter the name of a country: Mexico Enter the area in square km and population of Mexico: 1973000 122300000 Please enter the name of a country: Canada Enter the area in square km and population of Canada: 9985000 35160000 Please enter the name of a country: Liberia Enter the area in square km and population of Liberia: 111370 4294000 Please enter the name of a country: DONE
Liberia has the smallest area at 111370 square km. Canada has the largest area at 9985000 square km. Canada has the smallest population density at 3.521282 people per square km. Mexico has the largest population density at 61.986822 people per square km.
I don't know, I'm really confused with this Java program. I don't know how to do the main part of the program where the user inputs, array, and the output.
// Access the Scanner and ArrayList class by importing the java.util package. import java.util.Scanner;
/** Project - A Country Object * The purpose of this program is for the user to enter some information at most 10 countries. After the user has entered information about all the countries, the program should print out which countries in the list have the smallest and largest area and population density. */
public class Country { private String countryName; private int theArea; private int thePopulation; public Country(String countryName, int thePopulation, int theArea)
Modify the Improved Fibonacci application to store its sequence in an array. Do this by creating a new class to hold both the value and a boolean value that says whether the value is even, and then having an array of object references to objects of that class.
Did I just need to declaring the variable in other class (for boolean value and the value itself) or else ?
Here is the code for ImprovedFibonacci.java
Java Code:
class ImprovedFibonacci { static final int MAX_INDEX = 9; /** * Print out the first few Fibonacci numbers, * marking evens with a '*' */ public static void main(String[] args) { int lo = 1; int hi = 1; String mark;
I'm playing with vectors for the first time... What I'm trying to do is to allow a user to input one or more integers and store them in a vector for manipulation later on in the program... Here's the portion of the program I'm working with:
Java Code:
package com.itse2317; import java.util.*; public class VectorTest { public static void main(String[] args) { Scanner input = new Scanner(System.in);
[code]...
My question is this: Is there any way to move from inputting integers to printing them, without entering a non-integer (for example, hitting enter)? I looked at the API for the Vector class, and either I'm not thinking about the problem the right way to be able to find an answer, or it's just not there.
So I am trying to open a file and store the contents into a two dimensional array. I seem to have no problem using this same basic code to read the file and output it. But when I try to change it so that all the data is stored into the array, it does not work.
Java Code:
public void convertFile() { String filePath = ("C:UsersBradDownloadsFB1.csv"); String [][] rowCol = new String [429][6]; try { BufferedReader br = new BufferedReader(new FileReader(filePath)); StringTokenizer st = null; System.out.println("Your file is being converted to an array. This may take several minutes.");
public class InputFileData { /** * @param inputFile a file giving the data for an electronic * equipment supplier’s product range * @return an array of product details * @throws IOException */ public static Product [] readProductDataFile(File inputFile) throws IOException{ // YOUR CODE HERE }
This code is meant to be used to read a text file and store the data in an array of type Product[]. I know how to read in a text file and have it sort it into an array, but I've never seen code laid out in this fashion before (specifically "public static Product[]", and I'm unsure how to work with "(File inputfile)". I've looked all over the place but can't find any examples of anything like this.
Also, the code given cannot be changed, as it's that code I have to work with.
I still don't understand how to use it as a whole. For example, do I read the file in the main and have this method read that in and output to the Product class? Do I read the file in this method? I can't work out how to make this work when I have to use this method.
Write a java program to store employee information in a linked list, the program should implement the following functions:
The program should display the list of functions, and ask the user to enter the number of function that he/she wants to do, then perform the function as it is required in the previous table.
import java.util.*; public class Employee { Scanner in = new Scanner(System.in); String Name; String Address; String Department; int ID; int Salary;
[code]....
this is my out put
Please choose a number: 1-Add a new employee 2-Update employee's info 3-Remove employee's info 4-Exit 1 Enter name: Enter address: 2 Enter department: 3 Enter ID: 4 Enter salary:
now:
1- why are not my adding coming out in the output only the Enter name & Enter address ??
2- how can I add names and ID's and information to test that program
I have a college question ask me to write a class StringRevert which does the following:
-prompting user for an interger n -creating an array of n string -repeatedly read character string from user input and store them in the array until end of array is reached or user input -quit(quit should not saved in array) -print the string from array in reverse order, from last enter to first enter. -assume user always supplie correct input
This is what I've done so far but how to get it working.
import java.util.Scanner; public class StringRevert { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("enter value of n: "); int n1 = in.nextInt(); String[] n = new String[n1];
in my progrm there are three diff array of objects...namely garments..gadgets and home app...now one who buys from each of these sections will have to make a bill at last...when he choses to make the bill he will be shown the list of products he bought and their details (like price...brand...etc)...so i thought that while he orders each product(which is done in a previous method called purchase()...)....(each product is stored as an object in there diif arrays namely garments...gadgets ...appliances)....each of those object will be copied in a new array in a diif class...then that array print will give me the desired result...
is this approach correct...?and if its correct then how can i pull out a specific obj frm a stored array of object and then save it in a new array....?
The point of this program is to read characters from a txt file and store them into a 2D array. After this has been accomplished, the information is to be printed in the same manner it is read from in the txt file.
Here is the code I have so far:
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException
[Code] ....
And this is the error I am receiving when trying to accomplish the goal of the project:
Exception in thread "main" java.lang.NumberFormatException: For input string: "############" at java.lang.NumberFormatException.forInputString(Unk nown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Maze.<init>(Maze.java:15) at Main.main(Main.java:20)
What's going on here and how I can correct this?? The information I am trying to store in a 2D array and then print is this:
I have to store the name SD array and display the room and floor no. whose name is entered but i am having a problem with the logic part. The answer is coming to be wrong
import java .io.*; class hotel { public static void main()throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(read); String ar[][] = new String[5][10];