Printing out a Random Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Energizer100
    New Member
    • Nov 2007
    • 60

    Printing out a Random Array

    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...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • Energizer100
      New Member
      • Nov 2007
      • 60

      #3
      Originally posted by JosAH
      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

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        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:

        Code:
        void f() {
            void g() {
                
            }
        }
        instead of

        Code:
        void f() {
            
        }
        
        void g() {
                
        }

        Comment

        • Energizer100
          New Member
          • Nov 2007
          • 60

          #5
          Originally posted by BigDaddyLH
          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:

          Code:
          void f() {
              void g() {
                  
              }
          }
          instead of

          Code:
          void f() {
              
          }
          
          void g() {
                  
          }
          okay, one problem was fixed. But it still says "illegal start of expression" for the random array method.

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by Energizer100
            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.

            Comment

            • Energizer100
              New Member
              • Nov 2007
              • 60

              #7
              Originally posted by BigDaddyLH
              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);
              }
              }

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Your first line is wrong: you can't import a package, you import classes instead;
                as in:

                [code=java]
                import java.util.*;
                [/code]

                Compilers can be real nitpickers (which is AGT (A Good Thing (tm)))

                kind regards,

                Jos

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  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.

                  Comment

                  • Energizer100
                    New Member
                    • Nov 2007
                    • 60

                    #10
                    [code=java]
                    import java.util.Array s;

                    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);
                    }
                    }
                    [/code]

                    Still doesn,t work, same error
                    Last edited by JosAH; Dec 7 '07, 11:35 PM. Reason: added [code] ... [/code] tags

                    Comment

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      See reply #9 -- I think it was posted just as you were replying!

                      Comment

                      • Energizer100
                        New Member
                        • Nov 2007
                        • 60

                        #12
                        Originally posted by BigDaddyLH
                        See reply #9 -- I think it was posted just as you were replying!
                        Code:
                        import java.util.Arrays;
                        
                        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 void main(String args[])
                        	{
                        		int k;
                        		
                        		System.out.println("The random array is: " + randomArray(k));
                        	}
                        }
                        It says i have a possible loss of precision at a[i] = Math.random()

                        Comment

                        • Laharl
                          Recognized Expert Contributor
                          • Sep 2007
                          • 849

                          #13
                          Math.random() returns a decimal value in the range 0 <= x < 1. Thus, it is not an integer, but rather a double or float (I'm guessing double). To resolve this, you need to either use the Random class (google it) or cast Math.random() to an integer after multiplying to get the range you want.

                          For 0<=x<10 (aka random numbers less than 10):
                          [CODE=java]
                          import java.util.Rando m; //util.* covers this
                          public class A {
                          public static void main(String[] args){
                          int[] a = new int[8];
                          for (int b = 0; b < a.length; b++)
                          a[b] = (int)(Math.rand om()*10);
                          }
                          }
                          [/CODE]

                          Comment

                          • Energizer100
                            New Member
                            • Nov 2007
                            • 60

                            #14
                            Originally posted by Laharl
                            Math.random() returns a decimal value in the range 0 <= x < 1. Thus, it is not an integer, but rather a double or float (I'm guessing double). To resolve this, you need to either use the Random class (google it) or cast Math.random() to an integer after multiplying to get the range you want.

                            For 0<=x<10 (aka random numbers less than 10):
                            [CODE=java]
                            import java.util.Rando m; //util.* covers this
                            public class A {
                            public static void main(String[] args){
                            int[] a = new int[8];
                            for (int b = 0; b < a.length; b++)
                            a[b] = (int)(Math.rand om()*10);
                            }
                            }
                            [/CODE]
                            Code:
                            import java.util.Arrays;
                            
                            public class Compact 
                            {
                            	public static void main(String args[])
                            	{	
                            		int a[] = new int[20];	
                            		
                            		public static int[] randomArray(int a)
                            		{			
                            			for (int i = 0; i < a.size() ; i++)
                            			{
                            				a[i] = (int)(Math.random() * 10);
                            			}
                            			System.out.println("The random array is: " + a);
                            		}
                            	}
                            }
                            Still didn't work.

                            Comment

                            • Energizer100
                              New Member
                              • Nov 2007
                              • 60

                              #15
                              Code:
                              import java.util.Arrays;
                              
                              public class Compact 
                              {
                              	public static void main(String args[])
                              	{	
                              		int a[] = new int[20];	
                              
                              		for (int i = 0; i < a.length ; i++)
                              			{
                              				a[i] = (int)(Math.random() * 10);
                              			}
                              	
                              		System.out.println("The random array is: " + a);
                              	
                              	}
                              }
                              Okay, it has random stuff in it when it is printed out and it doesn't go to 20.

                              Comment

                              Working...