-3

This is my first time doing java and I am am trying to get the largest number from an array of x numbers using a method called bigNum(). Can anyone tell me why this doesn't work?

class project3
{
    public static void main(String args[])
    {
        int total =0;
        int b;

        System.out.println("How many numbers do you want in the array");
        int maxItems = EasyIn.getInt();
        int[] numbers = new int[maxItems];
        for (int i=0; i < maxItems; i++)
        {
            b = EasyIn.getInt();
        }
        bigNum(b);
    }

    public static void bigNum(int maxItems)
    {
        for (int i = 1; i >= maxItems; i++)
        {
            if (bigNum(b) >= maxItems)
                bigNum(b) = maxItems;
        }
        return bigNum(b);
    }
}
Sebastian Höffner
  • 1,864
  • 2
  • 26
  • 37
  • 2
    What is your program actually doing right now? – Sam I am says Reinstate Monica Mar 11 '14 at 15:12
  • At least, there is a complete mismatch of opening and closing braces – Andreas Fester Mar 11 '14 at 15:13
  • what have you tried? What's not working as you expected? Specifics will help get an answer. You probably want b = EasyIn.getInt() inside the {} before bigNum – Paul Rubel Mar 11 '14 at 15:14
  • saying illigale start of expression – user3406768 Mar 11 '14 at 15:15
  • 1
    The logic is totally off. You overwrite `b` each time in the loop, then you call `bigNum` recirsively in a loop, probably causing a stack overflow. Start with `numbers[i] = EasyIn...` and pass that numbers array to `bigNum`. Using proper indentation helps a lot, too. – tobias_k Mar 11 '14 at 15:16
  • G:\Computor IT with Programing\Java Programming\Assignment2\project3.java:20: error: illegal start of expression public static void bigNum(int maxItems) ^ G:\Computor IT with Programing\Java Programming\Assignment2\project3.java:20: error: illegal start of expression public static void bigNum(int maxItems) ^ – user3406768 Mar 11 '14 at 15:18
  • @user3406768 Read the answer from Sam I am. It explains the major issue. – Andreas Fester Mar 11 '14 at 15:19
  • int[] numbers [i] = new int[maxItems]; like that – user3406768 Mar 11 '14 at 15:21
  • the method is not working it is saying illegal start of expression on public static void bigNum(int maxItems) – user3406768 Mar 11 '14 at 15:37
  • possible duplicate of [Auto detect mobile browser (via user-agent?)](http://stackoverflow.com/questions/1005153/auto-detect-mobile-browser-via-user-agent) – Sheridan Mar 11 '14 at 16:29
  • `int[] numbers[i]` can not work. `int[] numbers;` declares a variable (numbers) as an int-array. `numbers[i]` accesses the i-th element of that array. So try `int[] numbers = new int[maxItems];`. edit: @Sheridan what do those two questions have in common? – Sebastian Höffner Mar 11 '14 at 17:26
  • is my calculation wrong in the method to get the biggest number in the if statment – user3406768 Mar 11 '14 at 18:02
  • Yes. As already pointed out you are calling `bigNum(b)` in a wrong way and you are probably also passing the wrong variable to the method (and your method can not return anything, since it's defined as `void`). Check my answer below for further explanations. – Sebastian Höffner Mar 11 '14 at 18:20

2 Answers2

1

You're probably getting compiler errors at this point due to unmatched braces. You want your program to have matched braces, and you also want to avoid having methods inside of other methods.

You want to have something that has the following form

class project3
{
    public static void main(String args[])
    {
        ...
    }

    public static int bigNum(int maxItems[])
    {
        ...
        return someInt;
    }   
}
0
// capital letter for the class (convention)
public class Project3 {
    public static void main(String args[]) {
        //int total = 0; // you never used this number

        System.out.println("How many numbers do you want in the array");
        int maxItems  = EasyIn.getInt();
        int[] numbers = new int[maxItems];

        for(int i = 0; i < maxItems; ++i) {
            int newNumber = EasyIn.getInt();
            /* you want to put the numbers into an array, 
               so don't call "bigNum" but put them there: */
            numbers[i] = newNumber;
        }
        // now find the big number:
        int bigNumber = bigNum(numbers);
        System.out.println("The biggest number: " + bigNumber);
    }

    // first:  change the return type to get the biggest number
    // second: pass the reference to the array, not a single number
    // public static void bigNum(int maxItems) {
    public static int bigNum(int[] items) {
        // create big number, assume it's very small:
        int bigNumber = Integer.MIN_VALUE;

        // this for loop will never run, change it a bit:
        //for(int i = 1; i >= maxItems; i++) {
        for(int i = 0; i < items.length; i++) {
            // your idea is correct, but you can not use the 
            // method here, see explanations below
            // Also don't check for the number of Items, but for
            if(items[i] > bigNumber) {
                bigNumber = items[i];
            }
        }

        return bigNumber;
    }
}

Explanations and further readings

Class name: Java has lots of different naming conventions, but the most common rules are: ClassNames/Types in CamelCase with a Capital at the beginning, variableNames following a similar convention but with a leading small letter. This makes it much easier to read code.

Indentation: Try to use a more consistent indentation. Also supports readability. Actually some other programming languages even rely on correct indentation.

Try to understand what variables and what methods are and how to use them (and return from them, you can not assign values to a method in Java. While you read the latter tutorial focus on return types and how to call methods correctly, you can not return an int when your method is of type void. Also the parameters need to be exactly defined.

Apart from that try to compile your code before you post it. As your code went, it should have thrown lots of compile errors, e.g. bigNum(b) = maxItems; should tell you that the left-hand side of an assignment needs to be a variable. This can help you a lot while tracking down mistakes.

Another error is that for most people EasyIn will not be defined (as it is for me, so the code I posted above might actually not be working, I didn't try). I suppose it's a learning library (we had our AlgoTools back in our first Java lectures). Still it would be nice to tell us what it is and what other imports you use (common mistake when I let my IDE decide my imports for me: java.util.Date and java.sql.Date).

Also try to make clear to yourself what you want to achieve with your program and how. Your algorithm actually looks like you didn't think too much about it: You try to find a biggest number and always check "a big number" against the number of expected items, which then might become "the big number" as well. Or something like that.

Programming is being concise and exact, so make a plan before. If it's too hard for you to think about a solution directly, you can maybe draw it on paper.

And if you then have problems, after compiling, asking your program, asking google, asking stack overflow: provide us with as many details as you can and we will be able to help you without just posting some code.

Good luck!

Sebastian Höffner
  • 1,864
  • 2
  • 26
  • 37
  • Thank you for the information the big number (bigNum()) was to call the method anywhere in the program more than once anywhere i needed it and was trying to figure out where i was going wrong not sure if i can use the return method because i have to call for the big number method again in the program most appreciated – user3406768 Mar 11 '14 at 18:23
  • thank you for you time sorry for all the confutation – user3406768 Mar 11 '14 at 18:27
  • You can reuse `public static int bigNum(int[] number)` everywhere. Just take any `int[]`, e.g. `int[] myNewNumbers = new int[]{12,52,533,41,52};` and call `int myBigNumber = bigNum(myNewNumbers);`. – Sebastian Höffner Mar 11 '14 at 18:33
  • And maybe a rule of thumb: Left of a `=` there are almost never any round brackets `()`, so if you happen to have that you probably mean `[]` for array access or messed up something. – Sebastian Höffner Mar 11 '14 at 18:35