Results 1 to 5 of 5
Thread: Can't figure out these errors
- 11-15-2012, 01:30 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Can't figure out these errors
What's wrong with this code?
I get the following errors: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; } }
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
- 11-15-2012, 01:46 PM #2
Godlike
- Join Date
- Nov 2012
- Posts
- 198
- Rep Power
- 1
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
and then fill in the blanks.Java Code:for ( ; ; ) { }
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.
- 11-15-2012, 02:59 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
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.
Again, I appreciate your help.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; } }
- 11-15-2012, 03:24 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-16-2012, 12:34 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
help me out am kinda new to java and can't figure out these errors..
By fireman in forum New To JavaReplies: 3Last Post: 03-16-2012, 11:07 PM -
can't figure it out myself
By Doyle Raymond in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-18-2011, 03:34 PM -
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
2 Errors Please help em figure out what is wrong with this
By jwb4291 in forum Advanced JavaReplies: 16Last Post: 08-09-2010, 11:40 PM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks