Having issues with arrays - java

I'm working on a homework problem that is asking me to write a program that generates 20 random numbers (0-99) in an array and then sorts and prints them.
I am getting crazy errors in my methods and I can't figure out whats wrong. I keep getting errors "Illegal start of expression, ; expected and .class expected. Any advice would be great.
import java.util.Arrays;
public class P6_14
{
public static void main(String[] args)
{
System.out.println("This program will sort randomly generated numbers");
public static int[] createNumbers(int n)
{
int[] numbers = new int[n];
for (int i = 0; i < n; i++)
{
numbers[i] = (int) (Math.random() * 99 + 1);
}
return numbers;
}
public static void orderArray(int[] array)
{
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}
}
}

The way your trying to fit everything into one main method isnt very OOP. Try to create methods in the future within your class! Hope this help
import java.util.Arrays;
public class Hello{
private int[] numbers = new int[20];
public Hello(){
for (int i = 0; i < this.numbers.length; i++)
{
this.numbers[i] = (int) Math.floor(Math.random()*99);
}
}
public void orderArray()
{
Arrays.sort(this.numbers);
}
public void printArray()
{
for (int i = 0; i < 20; i++)
{
System.out.println(this.numbers[i]);
}
}
public static void main(String[] args)
{
System.out.println("This program will sort randomly generated numbers1");
Hello test = new Hello();
System.out.println("Printing randomized");
test.printArray();
test.orderArray();
System.out.println("Printing sorted");
test.printArray();
}
}

Related

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;
}
}

How to reverse an array of numbers in java?

import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] xxx={1,3,5,7,9};
System.out.println(Arrays.toString(krakin(xxx)));
}
public static int[] krakin(int[]x) {
for(int i=x.length-1;i>=0;i--) {
int[]dev=new int[x.length-1];
dev[i]=x[i];
}
return dev[i];
}
I'm writing a method in java that reverses the order of the passed array.
I'm getting an error saying void is not allowed in my main method.
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] xxx={1,3,5,7,9};
System.out.println(krakin(xxx));
}
public static void krakin(int[]x){
for(int i=x.length-1;i>=0;i--){
}
}
Your krakin method has a void return type, which means it returns nothing. Therefore you can't pass it as an argument to System.out.println.
You can change it, for example, to return an int array:
public static int[] krakin(int[]x){
int[] rev = new int[x.length];
...
return rev;
}
Then you could print it in your main:
System.out.println(Arrays.toString(krakin(xxx)));
With additional libraries it can be done within one line.
If we consider pure Java then I would write like this:
public static void main(String[] args) {
int[] xxx = {1,3,5,7,9} ;
int[] reversed = reverseWithStream(xxx);
int[] reversed2 = reverseWithTempArray(xxx);
Arrays.stream(reversed).forEach(System.out::println);
Arrays.stream(reversed2).forEach(System.out::println);
}
private static int[] reverseWithStream(int[] array) {
return Arrays.stream(array)
.boxed()
.sorted(Collections.reverseOrder())
.mapToInt(value -> value)
.toArray();
}
private static int[] reverseWithTempArray(int[] sourceArray) {
int[] tempArray = new int[sourceArray.length];
for (int i = 0; i < tempArray.length; i++) {
tempArray[i] = sourceArray[sourceArray.length - 1 - i];
}
return tempArray;
}
One of the biggest benefits of this one is the fact that the previously created array is not affected.
In case of:
java.util.Arrays#sort(int[])
Or apache commons methods, the previously created array is affected.
public static void main(String[] args) {
int[] numbers = new int [100];
int j = numbers.length ;
for (int i = 0; i <= 99; i++) {
numbers [i] = numbers [i] + j ;
j = j - 1 ;
System.out.println(numbers [i]);
}

Manually sorting an ArrayList of strings alphabetically in Java

I am trying to reorder an ArrayList alphabetically for an assignment. I am not allowed to use any methods for automatically sorting, it has to be done manually. This is the code I tried, but it does not even run. I appreciate any input on this.
import java.util.ArrayList;
public class SortArrayList {
public static void main(String[] args) {
ArrayList<String> values = new ArrayList<String>();
public static void main(String[] args) {
ArrayList<String> values = new ArrayList<String>();
values.add("car");
values.add("bear");
values.add("apple");
values.add("xray");
sort(values);
for (int i = 0; i < values.size(); i++)
System.out.println(values.get(i));
}
public static void sort(ArrayList<String> x) {
String temp;
for (int i = 0; i < x.size() - 1; i++) {
for (int j = i + 1; j < x.size(); j++) {
if (x.get(i).compareToIgnoreCase(x.get(j)) > 0) {
temp = x.get(i);
x.add(i,x.get(j));
x.add(j,temp);
}
}
}
}
}
The lines
x.add(i,x.get(j));
x.add(j,temp);
are adding more elements to the ArrayList. You should change them to
x.set(i, x.get(j));
x.set(j, temp);
so that it replaces the elements at those positions.
Unless the multiple calls to public static void main(String[] args) was a typo when you copied it across for your question this would cause issues when running the program as it won't know where to start

Adding results from loop into Array?

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 :)

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