0

Okay, apologies now if you do not like my indentations (I'm a beginner)..

I can't get this code to compile. The assignment is to write two user defined methods, one (a boolean) that determines if a letter is a vowel, the other that changes a user inputted int (ex: 12345) to the reverse (54321) I am having such a hard time with this code. Suggestions and tips would be greatly appreciated.

UPDATED: New code:

import java.util.Scanner;

public class InClassModule8
{
  public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter a letter: ");
    String letter = keyboard.nextLine();
    System.out.println("Please enter an integer: ");
    int digit = keyboard.nextInt();
    System.out.println(isVowel(letter));
    System.out.println("The reversed digits are: " + reverseDigit(digit));
    }
}

  private static boolean isVowel(String line) 
  {
    line = (line != null) ? line.trim() : "";
    if (line.length() == 1) {
        switch (Character.toUpperCase(line.charAt(0))) 
        {
        case 'A': case 'E': case 'I': case 'O': case 'U':
            return true;
        }

    return false;
    }

  private static String reverseDigit(int val) 
  {
    StringBuilder sb = new StringBuilder(String.valueOf(val));
    return sb.reverse().toString();
  }

Errors right now:

6 errors found: File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java [line: 16] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java:16: class, interface, or enum expected File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java [line: 19] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java:19: class, interface, or enum expected File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java [line: 24] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java:24: class, interface, or enum expected File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java [line: 27] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java:27: class, interface, or enum expected File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java [line: 32] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java:32: class, interface, or enum expected File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java [line: 33] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/In_Class_Module_8.java:33: class, interface, or enum expected

Smit
  • 4,685
  • 1
  • 24
  • 28
Gianna Caruso
  • 113
  • 2
  • 3
  • 14
  • Count your parenthesis and curly braces, and make sure all match up. Make sure that your return statements are in the right location (one method definitely has a misplaced return statement). – Hovercraft Full Of Eels Oct 22 '14 at 21:08
  • 1
    I think this should be `if( letter = A|| .....` some thing like `"A".equalsIgnoreCase(letter)`??? – Smit Oct 22 '14 at 21:10
  • this is In_Class_Module_8 C naming style not Java. you should hava **InClassModule8** – Kick Buttowski Oct 22 '14 at 21:11

3 Answers3

1

Your main() has some extra calls. I think you just wanted something like

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter a letter: ");
    String letter = keyboard.nextLine();
    System.out.println("Please enter an integer: ");
    int digit = keyboard.nextInt();
    System.out.println(isVowel(letter));
    System.out.println("The reversed digits are: " + reverseDigit(digit));
}

To test if it's a vowel, let's trim the output and switch on the uppercase character like -

private static boolean isVowel(String line) {
    line = (line != null) ? line.trim() : "";
    if (line.length() == 1) {
        switch (Character.toUpperCase(line.charAt(0))) {
        case 'A': case 'E': case 'I': case 'O': case 'U':
            return true;
        }
    }
    return false;
}

To reverse the number, convert it to a String add it to a StringBuilder and then reverse() that,

private static String reverseDigit(int val) {
    StringBuilder sb = new StringBuilder(String.valueOf(val));
    return sb.reverse().toString();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You have an extra brace in reverseDigits()

    }
    return number;
    }
  }

The one after return number; is one too many.

  • I took that out and still have 6 "class, interface, or enum expected " errors – Gianna Caruso Oct 22 '14 at 21:40
  • You have a multitude of mismatched braces. The closing brace right after main() is breaking you up. `isVowel()` doe not have a closing brace. Please find an editor that can highlight the "other' brace and check your code. –  Oct 22 '14 at 21:47
0

Your code has an extra curly brace at line 48. Please remove that.

Also at line 33, the code return status should come after the closing else statement.

else 
{
  status = false;
}
return status;
Anish Thomas
  • 106
  • 1
  • 2