Results 1 to 14 of 14
- 11-16-2012, 03:12 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Assistance with homework assignment?
Here are the specs: Write a program that takes 10 integers as input. The program places the even integers into an array called evenList, the odd integers into an array called oddList, and the negative integers into an array called negativeList. The program displays the contents of the three arrays after all of the integers have been entered.
I know I have some big mistakes... I am still in progress, but my errors are making no sense to me. (For example, illegal start of type on my first for loop?)
Would appreciate some insight... Thanks!
C:\Program Files\mywork\classwork\ArrayStuffTC.java:9: error: illegal start of typeJava Code:import java.util.*; public class ArrayStuffTC { int[] inputList = new int[10]; int[] evenList = new int[10]; int[] oddList = new int[10]; int[] negList = new int[10]; for(int i=0; i < inputList.length; i++) { inputList[i] = scan.nextInt(); } for (i = 0 : inputList[]) if ((inputList[] % 2 == 0) & (inputList[] >= 0) (inputList[] = evenList[]); else if ((inputList[] % 2 != 0) & (inputList <= 0)) (inputList[] = oddList[]); else (inputList[] = negList[]); System.out.println(evenList[] + "are the even numbers \n" + oddList[] "are the odd numbers \n" + negList[] + "are the negative numbers"); }
for(int i=0; i < inputList.length; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:9: error: ')' expected
for(int i=0; i < inputList.length; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:9: error: illegal start of type
for(int i=0; i < inputList.length; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:9: error: <identifier> expected
for(int i=0; i < inputList.length; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:9: error: ';' expected
for(int i=0; i < inputList.length; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:9: error: > expected
for(int i=0; i < inputList.length; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:9: error: '(' expected
for(int i=0; i < inputList.length; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:9: error: <identifier> expected
for(int i=0; i < inputList.length; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:9: error: illegal start of type
for(int i=0; i < inputList.length; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:9: error: <identifier> expected
for(int i=0; i < inputList.length; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:10: error: ';' expected
{
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:11: error: illegal start of type
inputList[i] = scan.nextInt();
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:11: error: ';' expected
inputList[i] = scan.nextInt();
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:11: error: <identifier> expected
inputList[i] = scan.nextInt();
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:14: error: class, interface, or enum expected
for (i = 0 : inputList[])
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:17: error: class, interface, or enum expected
else if ((inputList[] % 2 != 0) & (inputList <= 0))
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:19: error: class, interface, or enum expected
else
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:22: error: class, interface, or enum expected
System.out.println(evenList[] + "are the even numbers \n" + oddList[] "are the odd numbers \n" + negList[] + "are the negative numbers");
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:24: error: class, interface, or enum expected
}Last edited by compscistudent1; 11-16-2012 at 03:20 AM.
-
Re: Assistance with homework assignment?
To help us please,
- Show all error messages.
- Edit your post above to wrap your code in [code] [/code] tags so that it can be readable.
-
Re: Assistance with homework assignment?
OK, thanks for the edit. You're trying to call methods in the class, and that can't be done. In the class itself is where you declare variables and methods, but not where you call them (unless you're only doing it to initialize the variable. Perhaps you forgot to create a main method and put this code in there?
- 11-16-2012, 04:41 AM #4
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Assistance with homework assignment?
Well, there's my first dumb mistake... Thank you!
Now I am down to less errors, but I still don't understand them.
C:\Program Files\mywork\classwork\ArrayStuffTC.java:17: error: '.class' expectedJava Code:import java.util.*; public class ArrayStuffTC { public static void main(String[] args) { Scanner reader = new Scanner(System.in); int[] inputList = new int[10]; int[] evenList = new int[10]; int[] oddList = new int[10]; int[] negList = new int[10]; for (int i = 0; i < inputList.length; i++) { inputList[i] = reader.nextInt(); } for (i = 0; i < inputList[]; i++) {if ((inputList[i] % 2 == 0) & (inputList[i] >= 0)) inputList[i] = evenList[i]; else if ((inputList[i]%2 != 0) & (inputList <= 0)) inputList[i] = oddList[i]; else inputList[i] = negList[i]; } System.out.println(evenList[] + "are the even numbers \n" + oddList[] "are the odd numbers \n" + negList[] + "are the negative numbers"); } }
for (i = 0; i < inputList[]; i++)
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:26: error: '.class' expected
System.out.println(evenList[] + "are the even numbers \n" + oddList[] "are the odd numbers \n" + negList[] + "are the negative numbers");
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:26: error: '.class' expected
System.out.println(evenList[] + "are the even numbers \n" + oddList[] "are the odd numbers \n" + negList[] + "are the negative numbers");
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:26: error: '.class' expected
System.out.println(evenList[] + "are the even numbers \n" + oddList[] "are the odd numbers \n" + negList[] + "are the negative numbers");
^
C:\Program Files\mywork\classwork\ArrayStuffTC.java:26: error: ';' expected
System.out.println(evenList[] + "are the even numbers \n" + oddList[] "are the odd numbers \n" + negList[] + "are the negative numbers");
Any more insight? Thanks.
-
Re: Assistance with homework assignment?
The error message doesn't make sense for the errors that you're displaying. But your for loop won't work since you're trying to use a variable that doesn't exist at that point in the code. The i variable has not been declared for the second for loop, and so really doesn't exist there. Declare it in the for loop just as you do in the previous for loop.
- 11-16-2012, 04:49 AM #6
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Assistance with homework assignment?
Thanks, but that didn't fix any of the errors.
If you couldn't tell, we're just beginning arrays, so I'm pretty amateur at them. In the second for loop, does my code / assignment of array positions even make sense? I feel that they are incorrect, but I don't know how to fix them.
- 11-16-2012, 04:52 AM #7
Re: Assistance with homework assignment?
Furb mentioned that you never gave your i variable a type, so do that:
and then go from thereJava Code:for(int i=0; //...
- 11-16-2012, 05:04 AM #8
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Assistance with homework assignment?
I did. However, I got the same errors...
- 11-16-2012, 05:27 AM #9
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Assistance with homework assignment?
Here is where I am at. This compiles, accepts my inputs... but gives me horrible output.Java Code:import java.util.*; public class ArrayStuffTC { public static void main(String[] args) { Scanner reader = new Scanner(System.in); int[] inputList = new int[10]; int[] evenList = new int[10]; int[] oddList = new int[10]; int[] negList = new int[10]; System.out.println("Input 10 integers: "); for (int i = 0; i < inputList.length; i++) { inputList[i] = reader.nextInt(); if ((inputList[i] % 2 == 0) & (inputList[i] >= 0)) inputList[i] = evenList[i]; else if ((inputList[i]%2 != 0) & (inputList[i] <= 0)) inputList[i] = oddList[i]; else inputList[i] = negList[i]; } System.out.println("Even numbers: " + evenList + "\nOdd numbers: " + oddList + "\nNegative numbers: " + negList); } }
Even numbers: [I@72945e31
Odd numbers: [I@6ab41dbb
Negative numbers: [I@570c16b7
for example.
- 11-16-2012, 05:53 AM #10
Re: Assistance with homework assignment?
Ah, You're trying to print objects instead of values. You can't do it like that.
If you want to print all the elements in an array, you need to loop through the array and print the values contained therein.
- 11-16-2012, 05:57 AM #11
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Assistance with homework assignment?
Ah, I see. I'll try.
Would you mind to show me how, using this?
I will check back after I attempt...
- 11-16-2012, 06:06 AM #12
Re: Assistance with homework assignment?
Sure, so for example, if we had some array of ints like so:
we could write a simple method to neatly print an array like this:Java Code://... int[] nums = {5,2,6,2,1};
Which produces: [5,2,6,2,1]Java Code://... public void printIntArray(int[] array){ System.out.print("["); for(int i=0; i<array.length; i++){ System.out.print(array[i]); if(i < array.length - 1){ System.out.print(","); } } System.out.print("]") }
- 11-16-2012, 06:12 AM #13
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Assistance with homework assignment?
I understand how to do it with just one array, but how can I separate them into my three arrays without printing each value separately?
- 11-16-2012, 06:50 AM #14
Similar Threads
-
Homework Assignment
By Michael305rodri in forum New To JavaReplies: 1Last Post: 10-02-2012, 06:51 AM -
Homework Assignment, Need some help
By xdrazkalnytex in forum New To JavaReplies: 3Last Post: 04-28-2012, 12:56 AM -
Homework assignment: NEED HELP Please.
By cinderelladreams in forum New To JavaReplies: 4Last Post: 04-27-2012, 07:06 PM -
Homework Assignment, Need some help
By xdrazkalnytex in forum New To JavaReplies: 17Last Post: 04-11-2012, 05:56 PM -
assistance w/ java programming assignment
By clemsontigers in forum New To JavaReplies: 4Last Post: 04-07-2011, 08:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks