Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: unknown error

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default unknown error

    //7. Write a class called RandomNumbers to do the following:

    //a. Write a method called generate to randomly generate a 1000 numbers in the range [50..100]. Find and return the sum of these numbers.
    //b. Write a method called output to receive the sum. Output the sum



    import java.util.*;
    public class RandomNumbers
    {

    // declarations
    int sum=0;

    public static void main (String[]a)
    {
    // method to generate a 1000 numbers in the range [50..100]and sum up those numbers

    public double random() <<<< that's where I get an error but I "illegal start of expression"
    {
    for(int i=1;i<=1000;i++)
    {
    int number=(int)(math.random()*51)+100;
    }
    sum=sum+number;
    return sum;
    }
    }

    public void output(int sum)
    {
    System.out.print("The sum is :"+sum);

    }
    }


    can you pick up an error above?? please help


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    584
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: unknown error

    Jeebus! Use code tags to format your code!
    You cannot implement a method inside another method.

  3. #3
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: unknown error

    If you really want to make methods inside methods, consider functional programming

  4. #4
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: unknown error

    now I am confussed

    --- Update ---

    can you tell me what axactlly do I need to change ,just a clue

  5. #5
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: unknown error

    No, we can't, you must figure it out by ourself. If methods-within-methods are impossible, but you still need that method, then?.. What are your suggestions?

  6. #6
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: unknown error

    is it possible to find the highest number using for loop ( looking AT number 9a)


    9. Write a class called Marks to do the following:
    a. Write a method called findHi to input 10 marks. Find and return the highest mark.
    b. Write a method called output to receive and output the highest mark.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: unknown error

    Quote Originally Posted by njabulo ngcobo View Post
    is it possible to find the highest number using for loop ( looking AT number 9a)
    What happened when you tried it?
    Post the code with your questions if you have any

  8. #8
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: unknown error

    this is what i tryed

    import java.util.*;
    public class Marks
    {
    public double findHi()
    {
    int Marks=0;


    for(int i=1;i<=10;i++)
    {
    Scanner console=new Scanner(System.in);
    System.out.println("Enter a mark");
    Marks =console.nextInt();

    //totalCost=price*5;
    }
    return Marks ;

    }

    public void output(int Marks)
    {
    System.out.println("The highest mark is : " +Marks);

    }

    }

  9. #9
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: unknown error

    Consider [code=Java] /* your code here */ [//code] (the latter with ONE slash) to automatically highlight your code here.
    What was your reasoning for developing this for loop's logic? As I see, your algorithm of "for" is following:
    1. Before for loop create a variable Marks (btw, why starts with capital latter and why the name is identical to class's one?)
    2. In for loop
    2.1. Read a mark
    2.2. Save it to Marks (overwrite it)
    3. Return Marks
    Why shouldn't it work? What algorithm should work?

Similar Threads

  1. java.rmi.unknownhostexception :unknown host: pc error
    By hgauravnayak in forum Exceptions
    Replies: 0
    Last Post: May 22nd, 2013, 03:58 PM
  2. Getting an error! String.charAt(Unknown Source) ???
    By pikapo0 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: March 16th, 2013, 05:54 AM
  3. unknown problems
    By kindk12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 5th, 2012, 01:50 PM
  4. WHAT IS WRONG??!?!? unknown error
    By ineedhelpasap in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 5th, 2012, 01:07 AM
  5. [SOLVED] Unknown Character
    By aussiemcgr in forum Java Theory & Questions
    Replies: 19
    Last Post: September 1st, 2010, 05:22 PM