Results 1 to 9 of 9
  1. #1
    ETBunce is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Method Declaration Syntax Help

    I'm very new to Java, and I've created this code that prints out a string of random letters. I am using Net Beans, and I've added comments saying what the error message next to the line says.

    Java Code:
    package randomstring;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class RandomString {
    
        public static void main(String[] args) {
            
            public void newword() { //illegal start of expression
                
                Random rand = new Random();
                Scanner scan = new Scanner(System.in);
    
                int len = scan.nextInt();
                String word = "";
                String let = "";
                
                System.out.println("Choose the length of your word: ");
                
                for (int count = 1; count <= len; count++) {
    
                    int num = 1 + rand.nextInt(26);
    
                    switch(num) {
                        case 1:
                            let = "a";
                            break;
                        case 2:
                            let = "b";
                            break;
                        case 3:
                            let = "c";
                            break;
                        case 4:
                            let = "d";
                            break;
    //etc., etc., includes the cases for all letters.
    
                    }
                    word = word + let;
                }
                System.out.println("Word: " + word);
                newword();
            }
            newword(); //invalid method declaration; return type required
        }
    } //class, interface, or enum expected
    Some of this code used to work until I decided to put it in a method, so it can repeat over and over. I've had this exact same problem before with another program I wrote and I have no idea why it doesn't work. Any help, please? Thanks.
    Last edited by ETBunce; 12-02-2012 at 01:33 AM.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Method Declaration Syntax Help

    just "close" off the main method or put it all in there

    thats what caused illegal start.

    also i dont see why you were calling the method name in the method at the very end, what was the point of that?

    if you wanted to do that you need to put that in a method, you cant just have code on its own outside of a block

    update, it now compilies - change it to suit what you want it to do.

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class RandomString {
    
        public static void main(String[] args) {
        }
    
        public void newword() { //illegal start of expression
    
            Random rand = new Random();
            Scanner scan = new Scanner(System.in);
    
            int len = scan.nextInt();
            String word = "";
            String let = "";
    
            System.out.println("Choose the length of your word: ");
    
            for (int count = 1; count <= len; count++) {
    
                int num = 1 + rand.nextInt(26);
    
                switch(num) {
                    case 1:
                    let = "a";
                    break;
                    case 2:
                    let = "b";
                    break;
                    case 3:
                    let = "c";
                    break;
                    case 4:
                    let = "d";
                    break;
                    //etc., etc., includes the cases for all letters.
    
                }
                word = word + let;
            }
            System.out.println("Word: " + word);
            newword();
        }
    
         //invalid method declaration; return type required
    
    } //class, interface, or enum expected

  3. #3
    ETBunce is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Method Declaration Syntax Help

    I am no longer getting errors, but when I run the program in Net Beans, nothing happens. This is now my code:

    Java Code:
    package randomstring;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class RandomString {
    
        public static void main(String[] args) {
            newword();
        }
            
            public static void newword() {
                
                Random rand = new Random();
                Scanner scan = new Scanner(System.in);
    
                int len = scan.nextInt();
                String word = "";
                String let = "";
                
                System.out.println("Choose the length of your word: ");
                
                for (int count = 1; count <= len; count++) {
    
                    int num = 1 + rand.nextInt(26);
    
                    switch(num) {
                        case 1:
                            let = "a";
                            break;
                        case 2:
                            let = "b";
                            break;
                        case 3:
                            let = "c";
                            break;
                        case 4:
                            let = "d";
                            break;
    
    //blah
                    }
                    word = word + let;
                }
                System.out.println("Word: " + word);
                newword();
            }
        }
    Edit: Actually, when I run the program, the output box in Net Beans says "run: " and allows me to input something. When I do, and hit the enter key, I get some big error message.
    Last edited by ETBunce; 12-02-2012 at 01:51 AM.

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Method Declaration Syntax Help

    post the error message?

    i didnt do anything to the code, i only formatted so it would compile :)

  5. #5
    ETBunce is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Method Declaration Syntax Help

    This is the whole output after running the code:

    run:
    s <--I entered the "s"
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at randomstring.RandomString.newword(RandomString.jav a:17)
    at randomstring.RandomString.main(RandomString.java:9 )
    Java Result: 1
    BUILD SUCCESSFUL (total time: 20 seconds)

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Method Declaration Syntax Help

    you are telling me when the scanner asked for input you entered "s"

    this will obviously throw an exception because s in not an int, it is a string.

    also why dou you have the System.out.print("choose length xxxxxxx"); after you have entered the value of length?

    shouldnt it come before so the user knows what to do?

    think about how you would resolve this.

    your code basically goes:

    please user give me a number that i can store as the string length:

    ok system i choose "s" as my number

    WHAT!?! none of that thank you, i said a number! - miss match.

    you see what i mean?

  7. #7
    ETBunce is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Method Declaration Syntax Help

    Haha yes thanks, I was confused because it didn't ask me to choose a length, and so I thought it was a glitch or something with the scanner, and I thought the program was not actually running, and that's why it allowed an input. Of course that was not the case.
    Basically, that was the only problem, like you said, I made it ask for a length after the input. I switched them around now, and it works great. Thanks for your help!

  8. #8
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Method Declaration Syntax Help

    no problem :) just in future if you get the misMatch, just check the value you input is the same as what you are setting it as.

    you could do a "do while" "trycatch block" on the input to prevent this

    Java Code:
    int value;
            do{ 
                try {
                    System.out.print("enter value of art piece \n");
                    String i = scan.nextLine();
                    value = Integer.parseInt(i);
                    break;
                }
                catch (Exception e)
                {
                    System.out.println("Couldn't parse input, please eneter a valid number");
                }
            }while (true);
    This makes sure the input is an int

  9. #9
    ETBunce is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

Similar Threads

  1. invalid method declaration
    By mcfer in forum New To Java
    Replies: 8
    Last Post: 09-22-2012, 02:26 PM
  2. Replies: 1
    Last Post: 10-17-2011, 12:45 PM
  3. invalid method declaration error
    By bsarules in forum Java Applets
    Replies: 7
    Last Post: 05-25-2010, 07:06 PM
  4. BubbleSorting Problem: invalid method declaration
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 12-02-2009, 04:47 AM
  5. Error: invalid method declaration
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 01:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •