Adding results from loop into Array? - java

I am currently just mocking about with Java. To get some learning in, and of course the easiest way to learn is by asking.
In this section I've created a loop to give me 50 random numbers. What I want to do, is to compare these numbers later on. That's why I want to move all the numbers into an array. I have no clue how. I've tried different stuff, but my syntax is wrong. Can somebody tell me how to do this?
Code:
package project.main;
import java.util.Random;
public class Main {
public static int numbers[];
public static final void main(String []args) {
getRandom();
recheckNumbers();
}
public static void getRandom() {
Random RandomNumber = new Random();
for(int i = 1; i <= 50; ++i){
int randomInt = RandomNumber.nextInt(100);
System.out.println("Generated : " + randomInt);
}
}
public static void recheckNumbers() {
if(numbers[0] < numbers[1]) {
System.out.println("numbers[0] is biggest");
} else {
System.out.println("numbers[1] is biggest");
}
}
}
I just rewrote it a bit. Im now running into another issue at line 14. which is the numbers[i] = randomInt part.
Heres the new code..
package project.main;
import java.util.Random;
public class Main {
public static int numbers[];
public static final void main(String []args) {
Random RandomNumber = new Random();
for(int i = 0; i <= 49; ++i){
int randomInt = RandomNumber.nextInt(100);
numbers[i] = randomInt;
}
}
}

for(int i = 0; i <= 49; ++i){
int randomInt = RandomNumber.nextInt(100);
numbers[i] = randomInt;
System.out.println("Generated : " + randomInt);
}
After that you can loop through to get number
for(int i = 0; i <= 49; ++i){
System.out.println("Generated : " + numbers[i]);
}
Solution to new question
import java.util.Random;
public class Main {
public static int[] numbers = new int[50];
public static void main(String[] args) {
Random RandomNumber = new Random();
for(int i = 0; i <= 49; ++i){
int randomInt = RandomNumber.nextInt(100);
numbers[i] = randomInt;
}
}
}

The solution lemon provided is correct.
Additionally I want to point you to a mistake you did in your recheckNumbers method. You check if numbers[0] is smaller than numbers[1] and print out that numbers[0] is the biggest in the if block. You should switch the output from the if and else block to return the correct answer.

You need to say the size of the array. Here is my solution to your problem:
public class Main {
public static int numbers[] = new int[50];
public static void main(String[] args) {
getRandom();
recheckNumbers();
}
public static void getRandom(){
Random randomNumber = new Random(); // variables should start with lower case
for(int i = 0; i < 50; i++){
int randomInt = randomNumber.nextInt(100); // generate a random integer in the (0-99) interval
System.out.println("Generated: " + randomInt); // print the generated number
numbers[i] = randomInt; // put it in the array
}
}
public static void recheckNumbers(){
if(numbers[0] > numbers[1]){
System.out.println("numbers[0] is bigger");
} else {
System.out.println("numbers[1] is bigger");
}
}
}
Hope it helps :)

Related

When compiling program I get the error 'void' type not allowed here

I'm trying to create a program that asks for 10 integers and puts those numbers into an array of negative, positive, and odd arrays. In the end, I want the program to print out 3 rows of numbers that separate the users 10 numbers into "odd", "even", and negative". When I run this I get "error: 'void' type not allowed here"
import java.util.Scanner;
public class ArrayPractice{
private static void showArray(int[] nums)
{
for (int i=0; i<nums.length;i++)
{
if(nums[i]!=0)
{
System.out.println(nums[i] + " ");
}
}
}
public static void main(String[] args){
int evenArray[] = new int[10];
int evenCount = 0;
int oddArray[] = new int[10];
int oddCount = 0;
int negArray[] = new int[10];
int negCount = 0;
Scanner input = new Scanner(System.in);
for(int i = 0; i<10; i++)
{
System.out.println("Number? " + (i + 1));
int answer = input.nextInt();
if(answer<0)
{
negArray[negCount++] = answer;
}
else
{
if (answer % 2 == 0)
{
evenArray[evenCount++] = answer;
}
else
{
oddArray[oddCount++] = answer;
}
}
}
System.out.println(showArray(evenArray));
System.out.println(showArray(oddArray));
System.out.println(showArray(negArray));
}
}
showArray is void, it does not return anything. And, on inspection, it prints in the method itself. So this
System.out.println(showArray(evenArray));
System.out.println(showArray(oddArray));
System.out.println(showArray(negArray));
should just be
showArray(evenArray);
showArray(oddArray);
showArray(negArray);

why am I getting the cannot find symbol when passing an array into a method

can someone please tell me why I am getting this error when compiling?
import java.util.*;
import java.io.*;
public class StatsCalculator
{
public static void main (String[]args)
{
programHeader();
randomNo(random);
printArray(random);
}
public static void programHeader()//writes program header
{
System.out.println("****************");
System.out.println("Stats calculator");
System.out.println("****************");
}
public static int[] randomNo(int[] random)// fills an array with 10 random numbers
{
random = new int[10];
for (int i=0; i< random.length; i++){
int randomNumber= (int) (Math.random()*10)+1;
random[i] = randomNumber;
}
return random;
}
public static int[] printArray (int[] random)//prints array
{
System.out.println("Your ten random values are: ");
for (int i=0; i<random.length; i++){
System.out.print(Arrays.toString(random));
}
return random;
}
}
I am writing a simple program to fill and array with 10 random numbers 1-10 and then calculate the sum, mean, mode and median of all the random numbers but i can get the methods to work just to fill the array and to print the array.
any help is appreciated.
You should get return value of randomNo() then pass it to next method. This may help you:
import java.util.Arrays;
public class StatsCalculator {
public static void main(String[] args) {
programHeader();
int[] random = randomNo();
printArray(random);
}
public static void programHeader()//writes program header
{
System.out.println("****************");
System.out.println("Stats calculator");
System.out.println("****************");
}
public static int[] randomNo()// fills an array with 10 random numbers
{
int[] random = new int[10];
for (int i = 0; i < random.length; i++) {
int randomNumber = (int) (Math.random() * 10) + 1;
random[i] = randomNumber;
}
return random;
}
public static int[] printArray(int[] random)//prints array
{
System.out.println("Your ten random values are: ");
for (int i = 0; i < random.length; i++) {
System.out.print(Arrays.toString(random));
}
return random;
}
}

Array not printing or going into array?

So I have to generate an array (size inputed by User) with a limit of 0-9; but each time I try to
Array.toString()
it compiles with error. Even when I imported arrays that the compiler states that I can't import or resolve.
Start
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Frequency
{
int var1,var2,var3,var4,max,min,var5,var6,var7,var8;
int [] a;
Scanner dab = new Scanner(System.in);
public void rand()
{
Random rande = new Random();
System.out.println("How many Random Values ");
var1 = dab.nextInt();
for (int k = 0; k > var1; k++)
{
a[k] = (int)(Math.random() * 10);
}
System.out.println(Arrays.toString(a));
}
}
Tester:
public class FrequencyTester
{
public static void main(String[]args)
{
Frequency yeet = new Frequency();
yeet.rand();
System.out.println("test");
}
}
Your first mistake is in For loop k must be less than var1
for (int k = 0; k < var1; k++)
try this
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
class Frequency
{
int var1,var2,var3,var4,max,min,var5,var6,var7,var8;
int [] a;
Scanner dab = new Scanner(System.in);
public void rand()
{
Random rande = new Random();
System.out.println("How many Random Values ");
var1 = dab.nextInt();
a=new int[var1];
for (int k = 0; k < var1; k++)
{
a[k] = (int)(Math.random() * 10);
}
System.out.println(Arrays.toString(a));
}
}
public class FrequencyTester
{
public static void main(String[]args)
{
Frequency yeet = new Frequency();
yeet.rand();
System.out.println("test");
}
}

Generate random integers with a range and place them into this array

I am working on a problem for 5 hours, and I searched in a book and on the Internet, but I still cannot solve this problem, so please help me to check what's wrong with the program. And the pic is the requirement for this program.
//imports
import java.util.Scanner;
import java.util.Random;
public class Lab09 // Class Defintion
{
public static void main(String[] arugs)// Begin Main Method
{
// Local variables
final int SIZE = 20; // Size of the array
int integers[] = new int[SIZE]; // Reserve memory locations to store
// integers
int RanNum;
Random generator = new Random();
final char FLAG = 'N';
char prompt;
prompt = 'Y';
Scanner scan = new Scanner(System.in);
// while (prompt != FLAG);
// {
// Get letters from User
for (int index = 0; index < SIZE; index++) // For loop to store letters
{
System.out.print("Please enter the number #" + (index + 1) + ": ");
integers[index] = RanNum(1, 10);
}
// call the printStars method to print out the stars
// printArray(int intergers, SIZE);
} // End Main method
/***** Method 1 Section ******/
public static int RanNum(int index, int SIZE);
{
RanNum = generator.nextInt(10) + 1;
return RanNum;
} // End RanNum
/***** Method 2 Section ******/
public static void printArray(int integers, int SIZE) {
// Print the result
for (int index = SIZE - 1; index >= 0; index--) {
System.out.print(integers[index] + " ");
}
} // End print integers
} // End Lab09
As Tim Biegeleisen and Kayaman said, you should put everything in the question and not just an external image.
You have a lot of errors in your code. Below the code will compile and run but I recommend you to take a look and understand what it has been done.
Errors:
If you are declaring a method, make sure you use { at the end of the declaration. You have:
public static int RanNum(int index, int SIZE);
Should be:
public static int RanNum(int index, int SIZE){
// Code here
}
You also should declare outside your main method so they can be accessed across the program.
If you are passing arrays as arguments, in your method the parameter should be an array type too.
You have:
public static void printArray(int integers, int SIZE) {
// Code her
}
Should be
public static void printArray(int[] integers, int SIZE) {
// Code her
}
Here is the complete code:
package test;
import java.util.Random;
import he java.util.Scanner;
public class Test {
//Local variables
public static final int SIZE = 20; //Size of the array
static int integers[] = new int[SIZE]; //Reserve memory locations to store integers
static int randomNumber;
static Random generator = new Random();
static String prompt;
static final String p = "yes";
static boolean repeat = true;
static Scanner input = new Scanner(System.in);
Test() {
}
/***** Method 1 Section ******/
public static int RanNum (int low, int high) {
randomNumber = generator.nextInt(high-low) + low;
return randomNumber;
} //End RanNum
/***** Method 2 Section ******/
public static void printArray(int[] intArray, int SIZE) {
//Print the result
for (int i = 0; i < SIZE; i++) {
System.out.print (intArray[i] + " ");
}
} //End print integers
public static void main (String [] arugs) {
do {
for (int i = 0; i < SIZE; i++) {
integers[i] = RanNum(1, 10);
}
printArray(integers, SIZE);
System.out.println("Do you want to generate another set of random numbers? Yes/No");
prompt = input.nextLine();
} while(prompt.equals(p));
}
}

Creating a random generated array in method and printing it

So I'm pretty sure I've correctly created a random integer generator that will put the integers into an array, although I am having trouble with my second method, that is supposed to print out the array.
My code:
import java.util.Random;
import java.util.Scanner;
public class Engine {
public int numDigits, numDigitsSet;
public int i;
public int[] secretNumber;
public Random randomNumberGenerator;
Scanner sc = new Scanner(System.in);
public void setNumDigits()
{
numDigitsSet = numDigits;
}
public int getNumDigits()
{
System.out.print("Enter the number of digits to use: ");
return numDigits = sc.nextInt();
}
public void generateNewSecret()
{
Random rand = new Random();{
for (int i=0; i<numDigitsSet; i++)
{
secretNumber[i]= rand.nextInt(10);
System.out.println("" + secretNumber[i]);
}
}
}
public int[] getSecretNumber()
{
for (int j=0; j<secretNumber.length; j++)
{
System.out.println("" + secretNumber[j]);
}
return secretNumber;
}
public void convertNumtoDigitArray()
{
String[] userGuessSplit = Player.userGuess.split(",");
int[] userGuessArray = new int[userGuessSplit.length];
for (int j=0; j<userGuessSplit.length; j++)
{
userGuessArray[j] = Integer.parseInt(userGuessSplit[j]);
}
}
}
For printing out the array, you can use the seriously convenient
Arrays.toString(array);
JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(int[])

Categories

Resources