Results 1 to 8 of 8
Thread: Arrays
- 04-10-2009, 09:54 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
Arrays
Can someone please, help me correct my errors and get my while structure working?
Errors I get:
java:28: while expected do System.out.println ("Enter a number between 0 and 15. (Press 16 to quit the program)");
java:30: ')' expected num = scan.nextLine();
Java Code:import java.util.Scanner; public class Occurrences { public static void main (String[] args) { Scanner num = new Scanner(System.in); //----------------------------------------- //Reads an arbitrary number of integers that //are in the range of 0 to 15 inclusive. //Counts how many occurrences of each are //entered. //---------------------------------------- int[] array = new int[15]; int index, num, next; while (num >= 0 || num <= 15) numbers[index] = next; { do System.out.println ("Enter a number between 0 and 15. (Press 16 to quit the program)"); num = scan.nextLine(); } //--------------------------------------------------- //display a histogram that shows the number of times //a number was entered in each of the following ranges: //0 - 5, 6 - 10, and 11 - 15. Show one asterisk for each //occurence entered. //---------------------------------------------------- String[] asterisk={"0- 5: ","6- 10: ","11- 15:"}; for (index=0;index<num;index++) { next=numbers[index]; if (next<5) asterisk[0] +="*"; else if (next<6) asterisk[1] +="*"; else if (next<15) asterisk[2] +="*"; } for (index=0;index<3;index++) System.out.println(asterisk[index]); } }
- 04-10-2009, 09:59 PM #2
do System.out.println...
what is do? do what? that's the error.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
-
I would just scrape your while loop and re-write it. You've got it completely ferschmuttered. For info on how to do while loops, please look here: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Best of luck.
- 04-11-2009, 03:29 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-11-2009, 03:44 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
I just edited and got fewer errors:Java Code:import java.util.Scanner; public class Occurrences { public static void main (String[] args) { Scanner scan = new Scanner(System.in); //----------------------------------------- //Reads an arbitrary number of integers that //are in the range of 0 to 15 inclusive. //Counts how many occurrences of each are //entered. //---------------------------------------- final int LIMIT = 15; int[] array = new int[LIMIT]; int num, next, index; while (num < 0 || num > 15) { System.out.println ("Enter a number between 0 and 15. (Press 16 to quit the program)"); num = scan.nextLine(); while (num !=16) { System.out.println ("Enter a number between 0 and 15. (Press 16 to quit the program)"); num = scan.nextLine(); } } //--------------------------------------------------- //display a histogram that shows the number of times //a number was entered in each of the following ranges: //0 - 5, 6 - 10, and 11 - 15. Show one asterisk for each //occurence entered. //---------------------------------------------------- String[] asterisk={"0- 5: ","6- 10: ","11- 15:"}; for (index=0;index<num;index++) { next=array[index]; if (next<5) asterisk[0] +="*"; else if (next<6) asterisk[1] +="*"; else if (next<15) asterisk[2] +="*"; } for (index=0;index<array.size;index++) System.out.println(asterisk[index]); } }
java:32: incompatible types
found : java.lang.String
required: int
num = scan.nextLine();
java:38: incompatible types
found : java.lang.String
required: int
num = scan.nextLine();
java:60: cannot find symbol
symbol : variable size
location: class int[]
for (index=0;index<array.size;index++)
My program is suppose to ask a user for a number between 0-15 inclusive, not allow the user to enter invalid data, when the user enters invalid data its suppose to display a histogram of the occurrences of the numbers entered and its suppose to use a while structure. And since I am using Scanner as the name of the class it should not be used as a variable that is receiving a value from the input stream. Thats why I am using
Which is were some of my errors are coming from. :(Java Code:int num; num = scan.nextLine()
- 04-11-2009, 03:52 AM #6
You want to take a look at the Scanner API.
-- Scanner.nextLine:String
-- Scanner.nextInt:int
You want to get an int, so you should use nextInt instead.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 04-11-2009, 08:45 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-11-2009, 03:02 PM #8
Your "while" conditions are not correct:
The above indicates that it should loop while num is less than zero and greater than 15: I think you want the following:Java Code:while (num [B][COLOR="Red"]<[/COLOR][/B] 0 || num [B][COLOR="red"]>[/COLOR][/B] 15)
Now, the above code indicates that the "while" loop will work while num is greater or equal than zero or less or equal to 15.Java Code:while (num [B][COLOR="Blue"]>=[/COLOR][/B] 0 || num [B][COLOR="blue"]<=[/COLOR][/B] 15)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Help!! With arrays
By ookie833 in forum New To JavaReplies: 8Last Post: 12-14-2008, 12:57 AM -
Arrays
By TheRocket in forum New To JavaReplies: 6Last Post: 12-10-2008, 06:00 PM -
Need help with 2D arrays...
By rrsv2 in forum New To JavaReplies: 3Last Post: 11-30-2008, 03:15 AM -
2D Arrays
By Major90 in forum New To JavaReplies: 6Last Post: 11-06-2008, 02:08 PM -
2D-Arrays
By kbyrne in forum New To JavaReplies: 1Last Post: 02-07-2008, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks