Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Can't figure out these errors

    What's wrong with this code?

    Java Code:
       public class MoreMethods {
          public static void main(String[] args) {
          
          //12 specifies the highest exponent for i
             printPowersOf2(12);
             System.out.println();
             System.out.println();
             printWordNumTimes("errors", 4);
             System.out.println();
             System.out.println(doubleIt(5));
             System.out.println();
             System.out.println(sum100(2, 4));
             System.out.println();
             printNumbers(15);
             System.out.println();
             System.out.println();
             printmoreNumbers();
             System.out.println();
             System.out.println();
             vertical("hey now");
             System.out.println();
             System.out.print(countQuarters(130) + " total quarter(s) returned");
          
          }
       
          public static void printPowersOf2(int a) {
             for ( int i = 0; i < a; i++ ) {
                double x = Math.pow(2, i);
                System.out.print(x + " ");
             }
          }
       
          public static void printWordNumTimes(String word, int x) {
             for ( int i = 0; i < x; i++ ) {
                System.out.println(word);
             }
          }
       
          public static int sumab(int a, int b) {
             for ( int i = a; i < b; i++ )
                int c = a + i;
             return c;
          }
       	
          public static int doubleIt(int a) {
             return a*2;
          }
       
          public static void printNumbers(int x) {
             for ( int i = 1; i <= x; i++ ) {
                System.out.print("[" + i + "]" + " ");
             }
          }
       
          public static void printmoreNumbers() {
             for ( int i = 1; i<= 10; i++ ) {
                System.out.print("[" + i + "]" + " ");
             }
          }
       
          public static void vertical(String verticalword) {
             for ( int a = 0; a < verticalword.length(); a++ ) {
                System.out.println(verticalword.charAt(a));
             }
          }
       
          public static int countQuarters(int x) {
             return (x%100)/25;
          }
       }
    I get the following errors:
    MoreMethods.java:41: '.class' expected
    int c = a + i;
    ^
    MoreMethods.java:41: not a statement
    int c = a + i;
    ^
    MoreMethods.java:41: illegal start of expression
    int c = a + i;
    ^
    MoreMethods.java:41: ';' expected
    int c = a + i;
    ^
    MoreMethods.java:41: not a statement
    int c = a + i;

    Thanks for your help!
    Last edited by technologythatlasts; 11-15-2012 at 02:57 PM. Reason: Added code tags

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    986
    Rep Power
    2

    Default Re: Can't figure out these errors

    - sum100 is not an existing method
    - you have no curly braces after the for-loop for (int i = a; i < b; i++) for Java executes the next line in a loop, but it does nothing but assign a value over and over again. Add curly braces. (And move int c out of that scope.)

    If you get that braces problem a lot, get used to typing
    Java Code:
    for ( ; ; ) { 
    }
    and then fill in the blanks.

    Last, use [ code] and [/ code] tags to format you code. I can't read your code like that. (Which also makes me eager to skip this question...)
    Last edited by SurfMan; 11-15-2012 at 01:48 PM.

  3. #3
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Can't figure out these errors

    Thanks a lot! It has fixed all but one error. Here is the last remaining error:

    MoreMethods.java:43: cannot find symbol
    symbol : variable c
    location: class MoreMethods
    return c;
    ^

    Here is the corrected code with the proper code tags; thanks for informing me about that.

    Java Code:
       public class MoreMethods {
          public static void main(String[] args) {
          
          //12 specifies the highest exponent for i
             printPowersOf2(12);
             System.out.println();
             System.out.println();
             printWordNumTimes("errors", 4);
             System.out.println();
             System.out.println(doubleIt(5));
             System.out.println();
             System.out.println(sumab(2, 4));
             System.out.println();
             printNumbers(15);
             System.out.println();
             System.out.println();
             printmoreNumbers();
             System.out.println();
             System.out.println();
             vertical("hey now");
             System.out.println();
             System.out.print(countQuarters(130) + " total quarter(s) returned");
          
          }
       
          public static void printPowersOf2(int a) {
             for ( int i = 0; i < a; i++ ) {
                double x = Math.pow(2, i);
                System.out.print(x + " ");
             }
          }
       
          public static void printWordNumTimes(String word, int x) {
             for ( int i = 0; i < x; i++ ) {
                System.out.println(word);
             }
          }
       
          public static int sumab(int a, int b) {
             for ( int i = a; i < b; i++ ) {
                int c = a + i;
    			}
             return c;
          }
       	
          public static int doubleIt(int a) {
             return a*2;
          }
       
          public static void printNumbers(int x) {
             for ( int i = 1; i <= x; i++ ) {
                System.out.print("[" + i + "]" + " ");
             }
          }
       
          public static void printmoreNumbers() {
             for ( int i = 1; i<= 10; i++ ) {
                System.out.print("[" + i + "]" + " ");
             }
          }
       
          public static void vertical(String verticalword) {
             for ( int a = 0; a < verticalword.length(); a++ ) {
                System.out.println(verticalword.charAt(a));
             }
          }
       
          public static int countQuarters(int x) {
             return (x%100)/25;
          }
       }
    Again, I appreciate your help.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Can't figure out these errors

    'c' is declared in the for-loop, so is not accessible outside of that loop.
    You need to declare 'c' outside of the loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Can't figure out these errors

    Quote Originally Posted by Tolls View Post
    'c' is declared in the for-loop, so is not accessible outside of that loop.
    You need to declare 'c' outside of the loop.
    Ah okay, thanks for that. I managed to fix all the errors now.

Similar Threads

  1. Replies: 3
    Last Post: 03-16-2012, 11:07 PM
  2. can't figure it out myself
    By Doyle Raymond in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-18-2011, 03:34 PM
  3. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  4. Replies: 16
    Last Post: 08-09-2010, 11:40 PM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 AM

Posting Permissions

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