Results 1 to 14 of 14
  1. #1
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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!


    Java 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");
    
    }
    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: ')' 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 04:20 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  4. #4
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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.

    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];
     
     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");
      }
    }
    C:\Program Files\mywork\classwork\ArrayStuffTC.java:17: error: '.class' expected
    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  6. #6
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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.

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Assistance with homework assignment?

    Furb mentioned that you never gave your i variable a type, so do that:
    Java Code:
    for(int i=0; //...
    and then go from there

  8. #8
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Assistance with homework assignment?

    I did. However, I got the same errors...

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

    Default Re: Assistance with homework assignment?

    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);
    
      }
    }
    Here is where I am at. This compiles, accepts my inputs... but gives me horrible output.
    Even numbers: [I@72945e31
    Odd numbers: [I@6ab41dbb
    Negative numbers: [I@570c16b7

    for example.

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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. #11
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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...

  12. #12
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Assistance with homework assignment?

    Sure, so for example, if we had some array of ints like so:
    Java Code:
    //...
    int[] nums = {5,2,6,2,1};
    we could write a simple method to neatly print an array like this:
    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("]")
    }
    Which produces: [5,2,6,2,1]

  13. #13
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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?

  14. #14
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Assistance with homework assignment?

    You can loop through all three arrays in a method like that. You can pass them all in as params if you like, and print columns of numbers - however you like!

Similar Threads

  1. Homework Assignment
    By Michael305rodri in forum New To Java
    Replies: 1
    Last Post: 10-02-2012, 07:51 AM
  2. Homework Assignment, Need some help
    By xdrazkalnytex in forum New To Java
    Replies: 3
    Last Post: 04-28-2012, 01:56 AM
  3. Homework assignment: NEED HELP Please.
    By cinderelladreams in forum New To Java
    Replies: 4
    Last Post: 04-27-2012, 08:06 PM
  4. Homework Assignment, Need some help
    By xdrazkalnytex in forum New To Java
    Replies: 17
    Last Post: 04-11-2012, 06:56 PM
  5. assistance w/ java programming assignment
    By clemsontigers in forum New To Java
    Replies: 4
    Last Post: 04-07-2011, 09:07 PM

Posting Permissions

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