473,671 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing out a Random Array

60 New Member
import java.util;

public class Compact
{
public static int[] randomArray (int n)
{
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = Math.random();
}
return a;
}

public static double[] printArray (double[] a)
{
for (int i = 0; i<a.length; i++)
{
return a[i];
}
}

public static void main(String args[])
{
System.out.prin tln("Test for printing " + printArray(a));
}
}


In the above code, what I am trying to do is create an array with random integers. The size of the array is 20. I don't know how to call the array...
Dec 6 '07 #1
47 3996
JosAH
11,448 Recognized Expert MVP
Have you tried to compile it and read what the compiler had to say about it? btw,
you can't call an array, you might yell 'yoohoo' at it but I'm sure it won't react nor
respond. Arrays are kind of stupid.

kind regards,

Jos
Dec 6 '07 #2
Energizer100
60 New Member
Have you tried to compile it and read what the compiler had to say about it? btw,
you can't call an array, you might yell 'yoohoo' at it but I'm sure it won't react nor
respond. Arrays are kind of stupid.

kind regards,

Jos

import java.util;

public class Compact
{
public static void main(String[] args)
{
public static int[] randomArray (int n)
{
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = Math.random();
}
System.out.prin tln (a);
}
}
}


this doesn't work for some reason, it says illegal start of expression at the randomArray method. and a semicolon is expected at the second to last bracket
Dec 6 '07 #3
BigDaddyLH
1,216 Recognized Expert Top Contributor
When your syntax is way off the mark, the resulting compiler messages may not be helpful, because the compiler is so confused. That is the case, here.

You nested the defintion of method randomArray inside method main. In other words, you wrote:

Expand|Select|Wrap|Line Numbers
  1. void f() {
  2.     void g() {
  3.  
  4.     }
  5. }
  6.  
instead of

Expand|Select|Wrap|Line Numbers
  1. void f() {
  2.  
  3. }
  4.  
  5. void g() {
  6.  
  7. }
  8.  
Dec 6 '07 #4
Energizer100
60 New Member
When your syntax is way off the mark, the resulting compiler messages may not be helpful, because the compiler is so confused. That is the case, here.

You nested the defintion of method randomArray inside method main. In other words, you wrote:

Expand|Select|Wrap|Line Numbers
  1. void f() {
  2.     void g() {
  3.  
  4.     }
  5. }
  6.  
instead of

Expand|Select|Wrap|Line Numbers
  1. void f() {
  2.  
  3. }
  4.  
  5. void g() {
  6.  
  7. }
  8.  
okay, one problem was fixed. But it still says "illegal start of expression" for the random array method.
Dec 7 '07 #5
BigDaddyLH
1,216 Recognized Expert Top Contributor
okay, one problem was fixed. But it still says "illegal start of expression" for the random array method.
I can only guess what your code looks like now. I suggest that when you are asking a question about a syntax error, you should post the relevant code.
Dec 7 '07 #6
Energizer100
60 New Member
I can only guess what your code looks like now. I suggest that when you are asking a question about a syntax error, you should post the relevant code.
srry

import java.util;

public class Compact
{
public static void main(String[] args)
{
public static int[] randomArray(int n)
{
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = Math.random();
}
}
System.out.prin tln (a);
}
}
Dec 7 '07 #7
JosAH
11,448 Recognized Expert MVP
Your first line is wrong: you can't import a package, you import classes instead;
as in:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
Compilers can be real nitpickers (which is AGT (A Good Thing (tm)))

kind regards,

Jos
Dec 7 '07 #8
BigDaddyLH
1,216 Recognized Expert Top Contributor
You are still nesting method randomArray inside method main. See reply #4.

ps. If you use code tags, your code will be more readable in this forum.
Dec 7 '07 #9
Energizer100
60 New Member
Expand|Select|Wrap|Line Numbers
  1. import java.util.Arrays;
  2.  
  3. public class Compact 
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         public static int[] randomArray(int n)
  8.         {
  9.             int[] a = new int[n];
  10.             for (int i = 0; i < a.length; i++)
  11.             {
  12.             a[i] = Math.random();
  13.             }
  14.         }
  15.             System.out.println (a);
  16.     }
  17. }
  18.  
Still doesn,t work, same error
Dec 7 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
2495
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
16
4208
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have shelled out on VC++ 6.0 compiling that same code is proving difficult. I am not too worried how I generate random numbers in c++, as long as it is sufficiently random. Can anybody help me out in getting what I want from VC++?
10
5973
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object System.Random iRnd = new System.Random(); string theNum = iRnd.Next(0,8).ToString(); lblAnswer.Text = iRnd.Next(0,8).ToString();
4
9261
by: Arif | last post by:
I C# code prints very slow as compared to a third party barcode printing software. That software prints approximately 10 labels in 2 seconds while my C# code prints 10 labels in 5 to 6 seconds. And this differences increases with the increase number of labels. The code is as follwods: Here rdr = OleDbDataReader Font is Times New Roman, 12pt
5
2075
by: jar13861 | last post by:
I'm confused on how to write a random array that will only generate 9 different numbers from 1-9. Here is what I have, but its only writing one number.... holder = new Array ( 9 ); var flag = true; var rannum = Math.floor( 1 + Math.random() * 9 ); for (var j = 0; j < 9; j++) {
6
4084
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if the user wants all pages will produce 3 pages. I want to offer the user the ability to select via the print dialog that only pages 1 and 2 of it are printed or possibly pages 1 and 3 but not 2. At the moment I can produce all three pages...
12
3218
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
6
2123
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and refilled at each turn of cycle. On my developing pc, the same sequence of random numbers was repeated from on turn of cycle to the other; I put some Application.DoEvents() and all gone well.
11
3017
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays of values. I get a random index value for each array so I can pull the data from them.
0
8481
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8400
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8672
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7441
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.