import java.util.Scanner;
public class LoopPatterns {

    /**
     * @param args
     */
    public static void main(String[] args) {
    public static double Largest {  
                Scanner s = new Scanner(System.in);
                int num, largest = 0;

                for ( int i = 1; i <= 10 ;i ++) {
                 System.out.print("Enter a number : ");
                 num = s.nextInt();
                 if ( num > largest)
                      largest = num;
                }
                System.out.println("Largest Number : " + largest);



    public static double First {

            String name1, name2, name3 ;
            boolean b, b1;
            int i, i1, i2,i3;
            Scanner keyboard = new Scanner(System.in);

            System.out.print("Enter first string: ");
            name1 = scan.nextLine();

            System.out.print("Enter second string: ");
            name2 = scan.nextLine();

            System.out.print("Enter third string: ");
            name3 = scan.nextLine();

            b = name1.equals(name2);
             // Check if identical ignoring case

    b1 = name1.equalsIgnoreCase(name2);

        i = name1.compareTo(name2);

    if (i < 0)
        {
            i1 = name1.compareToIgnoreCase(name3);

             if (i1 < 0)
                {
        System.out.println(name1);
        if (i1 > 0)
            {
            System.out.println(name3);
            i2 = name2.compareToIgnoreCase(name3);
                if (i2<0)
                {
                    System.out.println(name2);
                    System.out.println(name3);
                }
                if (i2>0)
                {
                    System.out.println(name3);
                    System.out.println(name2);
                }
                }
                }
    }
    else if (i > 0)
        {

            i1 = name2.compareToIgnoreCase(name3);

             if (i1 < 0)
                {
        System.out.println(name2);
        if (i1 > 0)
            {
            i2 = name1.compareToIgnoreCase(name3);
                if (i2<0)
                {
                    System.out.println(name1);
                    System.out.println(name3);
                }
                if (i2>0)
                {
                    System.out.println(name3);
                    System.out.println(name1);
                }
                }
                }
    }

                }
    }
    }

}

this is my code for calculating the largest number from a set of numbers and returing a string in reverse order what are my errors ?

Dani AI

Generated

The compiler errors come from three root causes in the posted code: method declarations inside main, malformed method headers (parameters without types and illegal modifiers), and misplaced/mismatched braces that change variable scope. The messages quoted in Post #3 (for example, "illegal modifier for parameter" and "syntax error on double") are exactly what the compiler reports when it encounters public static method headers in the middle of a method or when parentheses/braces are broken. There are also identifier mismatches (scan vs keyboard) and a type/return mismatch (declaring a method returning double while using int variables). is correct that each method must be closed before the next is declared; 's suggestion to label/indent closing braces makes these mistakes easier to spot.

A compact, working structure that separates concerns and avoids the errors:

import java.util.Scanner;
import java.util.Arrays;

public class LoopPatterns {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int largest = findLargest(s, 10);
        System.out.println("Largest Number : " + largest);

        s.nextLine(); // clear newline after numbers
        printSortedThreeNames(s);
        s.close();
    }

    public static int findLargest(Scanner s, int count) {
        int largest = Integer.MIN_VALUE;
        for (int i = 0; i < count; i++) {
            System.out.print("Enter a number: ");
            int num = s.nextInt();
            if (num > largest) largest = num;
        }
        return largest;
    }

    public static void printSortedThreeNames(Scanner s) {
        System.out.print("Enter first string: ");
        String n1 = s.nextLine();
        System.out.print("Enter second string: ");
        String n2 = s.nextLine();
        System.out.print("Enter third string: ");
        String n3 = s.nextLine();
        String[] arr = { n1, n2, n3 };
        Arrays.sort(arr, String.CASE_INSENSITIVE_ORDER);
        for (String name : arr) System.out.println(name);
    }
}

Quick checklist to avoid the original errors:

  • Do not declare a method inside another method — end main before starting other methods.
  • Each method parameter needs its own type (e.g., String name1, not String name1, name2).
  • Match return types and variable types (int vs double).
  • Initialize largest to Integer.MIN_VALUE or read the first number before the loop so negative inputs work.
  • Use a single Scanner for System.in (close it once at the end); avoid closing it early.
  • Indent and/or label closing braces (per ) so braces and scopes are obvious.
  • Copy/paste full compiler error lines when asking for help — they show exact line numbers and root cause.

Recommended Answers

All 5 Replies

what are my errors ?

Please post the full text of any error messages so we can see them.
Or if there are no error messages, please explain your problem.

The formatting for the posted code is very bad. Each nested pair of {}s should be indented 3-4 spaces so you can easily see the logic. There should not be a column of }s one under the other.

The ending "}" should be vertically in line beneath the line with the pairing "{"

For example:

  for(int i=0;i<A.length;i++){
    int c = 1;      // start with one

    for(int j=i+1;j<A.length;j++) {
      if (A[i]==A[j]) {
        c=c+1;
        System.out.println("c="+c +" A[i]="+A[i] + ", i=" + i + ", j="+j);
        if (c>(A.length/2)){
           return A[i];
        }
      }
    } //  end for(j)
  }  // end for(i)
import java.util.Scanner;

public class LoopPatterns {

    /**
     * @param args
     */
    public static void main(String[] args) {
        public static double Largest () {   
            Scanner s = new Scanner(System.in);
            int num, largest = 0;

            for ( int i = 1; i <= 10 ;i ++) {
                System.out.print("Enter a number : ");
                num = s.nextInt();
                if ( num > largest)
                    largest = num;
            }
            System.out.println("Largest Number : " + largest);



            public static double First (String name1, name2, name3 ;
                boolean b, b1;
                int i, i1, i2,i3;) {


                Scanner keyboard = new Scanner(System.in);

                System.out.print("Enter first string: ");
                name1 = keyboard.nextLine();

                System.out.print("Enter second string: ");
                name2 = keyboard.nextLine();

                System.out.print("Enter third string: ");
                name3 = keyboard.nextLine();

                b = name1.equals(name2);
                // Check if identical ignoring case

                b1 = name1.equalsIgnoreCase(name2);

                i = name1.compareTo(name2);

                if (i < 0)
                {
                    i1 = name1.compareToIgnoreCase(name3);

                    if (i1 < 0)
                    {
                        System.out.println(name1);
                        if (i1 > 0)
                        {
                            System.out.println(name3);
                            i2 = name2.compareToIgnoreCase(name3);
                            if (i2<0)
                            {
                                System.out.println(name2);
                                System.out.println(name3);
                            }
                            if (i2>0)
                            {
                                System.out.println(name3);
                                System.out.println(name2);
                            }
                        }
                    }
                }
                else if (i > 0)
                {

                    i1 = name2.compareToIgnoreCase(name3);

                    if (i1 < 0)
                    {
                        System.out.println(name2);
                        if (i1 > 0)
                        {
                            i2 = name1.compareToIgnoreCase(name3);
                            if (i2<0)
                            {
                                System.out.println(name1);
                                System.out.println(name3);
                            }
                            if (i2>0)
                            {
                                System.out.println(name3);
                                System.out.println(name1);
                            }
                        }
                    }
                }

            }
        }
    }
}

"line 25- illegal modifier for parameter first only final is permitted
line 12-illegal modifier for parameters only final is permitted
line 11-largest cannot be resorted to variable
line 11-syntax error on double @expected

Each arg in a methods definition must have a type, there is not a list of names with a type.
See: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

A method can not be defined inside of another method. Code the ending "}" for one method before starting the definiton for the next method.

You should copy and paste the error messages, not type them in. The posted list of error messages has left off important information.

Dan, one tip I find helpful for beginners is to label the end brackets. For example:

public class AnyClass {

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

    }//main

}//class
commented: Good idea. I use it a lot!!! +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.