Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Help im a java noob!

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help im a java noob!

    Hey everyone im stuck on a java problem and i could use some help
    public class Split
    {
    private ArrayList<Integer> numbers;
     
    public Split()
    {
    numbers = new ArrayList<Integer>();
    numbers.add(1);
    numbers.add(5);
    numbers.add(21);
    numbers.add(43);
    numbers.add(60);
    numbers.add(70);
    numbers.add(80);
    numbers.add(90);
    numbers.add(100);
    }
     
    public ArrayList<Integer> splt(int n);
    {
    for(int i =0;i<numbers.size();i++){
        if(i != n){
            return 0;
        }else if(i == n){
             numbers.IndexOf(i);
            }
     
        }

    what i need to do is type in a number, if its in the list i created above it splits the array at that position
    example:
    int = 70
    output = firstlist is {1,5,21,43,60}
    Secondlist is {70,80,90,100}
    i tried searching for the input number in the list if it is not there it returns zero if it is it returns the position of the element in the list


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Help im a java noob!

    Hello jonathanfox!

    if(i != n)
    You need to compare the input number (n) with the value which is stored in position i not i itself. Look at the ArrayList's get(...) method. Then you can find the position to split your list.

    Hope this helps.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help im a java noob!

    o ya sry stupid mistake, but how do i split the list in two? id say ill hav to make another two lists?

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Help im a java noob!

    Quote Originally Posted by jonathanfox View Post
    o ya sry stupid mistake, but how do i split the list in two? id say ill hav to make another two lists?
    If I were you I would make splt method return the index of the input number.
    Then you can create two more lists by copying the elements of the initial list.
    Spend some time to design your steps and you will work it out.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help im a java noob!

    import java.util.ArrayList;
    public class Split
    {
    private ArrayList<Integer> numbers;
     
    public Split()
    {
    numbers = new ArrayList<Integer>();
    numbers.add(1);
    numbers.add(5);
    numbers.add(21);
    numbers.add(43);
    numbers.add(60);
    numbers.add(70);
    numbers.add(80);
    numbers.add(90);
    numbers.add(100);
    }
     
    public ArrayList<Integer> Split(int n)
    {
     
     
     
        ArrayList<Integer> FirstList = new ArrayList<Integer>();
        ArrayList<Integer> SecondList = new ArrayList<Integer>();
        FirstList.addAll(numbers);
         SecondList.addAll(numbers);
         for(int i =0;i<FirstList.size();i++){
         while(FirstList.get(i)<= n){
             FirstList.remove(i);
            }
            for( i =0;i<SecondList.size();i++){
         while(SecondList.get(i)> n){
             SecondList.remove(i);
            }
     
     
     
    }
    }
    return FirstList;
    }
    }
    okay i tried a different approach, make two copys of the array and remove the elements that are = to or > than n for firstarray then oposite for secondarray. It compiles but doesnt work

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help im a java noob!

    i know i should return the second list aswell put im not sure how to theres a lot of small things in java i have to learn

  7. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Help im a java noob!

    Quote Originally Posted by jonathanfox View Post
    okay i tried a different approach, make two copys of the array and remove the elements that are = to or > than n for firstarray then oposite for secondarray. It compiles but doesnt work
    This is not a different approach. This code does something else (and it does it wrong!). I suggested you to redesign your program not change its objectives. Did you read my previous post?
    Start with the method that will return the index of the input number in the list (if there is). ie make the splt(int n) return an int not an ArrayList.
    Then create another method (void) that takes an int -the index- as a parameter an splits the initial list in two others with the int parameter as a breaking point.
    Hint: make the two sublists class members (not local variables) like the numbers list.

  8. #8
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Help im a java noob!

    Quote Originally Posted by jonathanfox View Post
    should return the second list as well [b]ut [I']m not sure how to ...
    Here's the thing: In Java a function can return (at most) one thing.

    If you want the function to "return" two things and the things are reference objects, you can send the function some "containers" and let the function fill them up

    First of all: Before writing code for the function, think (yes think) about how you are going to write a function that calls the Split() function. Think (really: think) about how you are going to test the program.

    I mean, you are going to do this (eventually) anyhow. I respectfully suggest that program design start at the top (the main() function of your program) and work your way down.

    There are many other ways to implement the functionality, but I'll start with the kind of Split function that I think you have in mind.

    import java.util.ArrayList;
     
    public class z // Or whatever name you want for your program.  I really
                   // wanted "Z" but that's reserved for Zorro.
                   // So...I will break with convention and use lower case "z"
    {
        public static void main(String [] args)
        {
            // Will (probably) eventually read the list from
            // a file (or, maybe, from user command line or
            // some such thing).
            // For now, just initialize it with hard-coding.
            ArrayList<Integer> numbers = new ArrayList<Integer>();
            numbers.add(1);
            numbers.add(5);
            numbers.add(21);
            numbers.add(43);
            numbers.add(60);
            numbers.add(70);
            numbers.add(80);
            numbers.add(90);
            numbers.add(100);
            ArrayList<Integer> first  = new ArrayList<Integer>();
            ArrayList<Integer> second = new ArrayList<Integer>();
            int n = 70;
     
            Split(numbers, n, first, second);
     
            System.out.println("Original: " + numbers);
            System.out.println("First   : " + first);
            System.out.println("Second  : " + second);        
        } // End of main function
     
        static void Split(ArrayList<Integer>a, int n, ArrayList<Integer> b, ArrayList<Integer> c)
        {
            b.clear(); // Make sure it's empty to start with.
            c.clear(); // Ditto
            //
            // Fill up the arrays b and c with items from array a as directed by the Program Specification.
            //
        } // End of Split function
    } // End of class definition

    Output could look like this:

    Original: [1, 5, 21, 43, 60, 70, 80, 90, 100]
    First   : [1, 5, 21, 43, 60]
    Second  : [70, 80, 90, 100]



    Quote Originally Posted by jonathanfox
    there [are] a lot of small things in java [I] have to learn
    There are no "small things" in Java. Every blasted, bloomin' thing is a Big Thing. The Good News: You don' t have to learn them all at once.


    Cheers!

    Z
    Last edited by Zaphod_b; July 24th, 2012 at 10:27 AM.

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help im a java noob!

    ive been thinking about this and i think i know a way, but is a function the same as a method? could i return a method ? or do i sound really stupid haha

  10. #10
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Help im a java noob!

    Quote Originally Posted by jonathanfox View Post
    . is a function the same as a method?
    The word "function" is generic and is used in lots of other programming languages, not just Java. In most general terms: A function is something that can take some arguments and can return a value. It may or may not do something useful in the meanwhile.

    A method in Java (and other languages with object-oriented constructs) is a term used for a function that is associated with a class. An object or instance method is associated with a particular object and inherently has an invisible parameter (called "this") that allows it to access data members and other methods of that object without having an explicit parameter for that object.

    There are also static methods for classes. A static method is not associated with a particular object (so it doesn't have the implicit "this" parameter). The java Math library class has static methods sin(), cos(), pow(), etc...

    Some other languages (C and C++ for example) have pointer data types, and it's possible in those languages to have a pointer variable whose value is the virtual runtime address of a function. That allows any part of the program to invoke a function by dereferencing that pointer. You can pass a function pointer to a function or method and a function or method can return a function pointer.

    Remember we are not talking about Java here. Java doesn't have pointers that user programs can work with.

    I wasn't there when Java was created, but in my mind I can almost hear the Java creators agree on something like: "Man, there are a ton of bad C programs out there that misuse pointers. So the first rule, and the most important, absolutely immutable precept of Java is: No Pointers!" (Or some such thing.) When I finish my Wayback Machine, I am going to confirm my suspicion. It was only about 20 years ago, and that's just about right for the initial shakedown cruise of the W.M.

    Now, graphics programs in C and C++ use function pointers, among other things, to make it easy to implement "callback" functions, and some kind of "callback" mechanism is usually considered to be required for any decently structured program dealing with, say, mouse clicks. In Java, it is possible to get the (almost) equivalent functionality by using a Java mechanism called an "interface." That's how Java graphics library functions (typically) do it. Do you really want to get into that at your present state of development? (A rhetorical question. I don't expect an answer. I don't need an answer. I don't want an answer.)


    Quote Originally Posted by jonathanfox View Post
    ...do i sound really stupid
    No, you don't sound really stupid. The ones that sound really stupid are people who sprinkle "lol" throughout their communications.

    Quote Originally Posted by jonathanfox View Post
    haha
    Well, a little hokey, but still not as trite as lol. Actually, I like a little "haha" from time to time but I usually reserve "lol" for tête-à-tête.


    About stupidity in general, I wish I had said it first:

    ""Men are born ignorant, not stupid;
    they are made stupid by education."
    ---Bertrand Russell


    Good old Bertie. What a hoot!

    Cheers!

    Z
    Last edited by Zaphod_b; July 24th, 2012 at 02:43 PM.

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help im a java noob!

    Quote Originally Posted by jonathanfox View Post
    Hey everyone im stuck on a java problem and i could use some help
    public class Split
    {
    private ArrayList<Integer> numbers;
     
    public Split()
    {
    numbers = new ArrayList<Integer>();
    numbers.add(1);
    numbers.add(5);
    numbers.add(21);
    numbers.add(43);
    numbers.add(60);
    numbers.add(70);
    numbers.add(80);
    numbers.add(90);
    numbers.add(100);
    }
     
    public ArrayList<Integer> splt(int n);
    {
    for(int i =0;i<numbers.size();i++){
        if(i != n){
            return 0;
        }else if(i == n){
             numbers.IndexOf(i);
            }
     
        }

    what i need to do is type in a number, if its in the list i created above it splits the array at that position
    example:
    int = 70
    output = firstlist is {1,5,21,43,60}
    Secondlist is {70,80,90,100}
    i tried searching for the input number in the list if it is not there it returns zero if it is it returns the position of the element in the list
    First, your code only returns a value when i != n.
    So after you add a return statement for your if-else think about this. What would be returned if you searched for 1?

  12. #12
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help im a java noob!

    hey left it a day and came to it and finally solved it thanks guys

Similar Threads

  1. Noob here - but ten years of Java and still learning
    By bolsover in forum Member Introductions
    Replies: 1
    Last Post: June 20th, 2012, 07:57 AM
  2. >>> Java Noob here <<<
    By Kikiam in forum Member Introductions
    Replies: 2
    Last Post: March 19th, 2012, 09:48 AM
  3. Please help! Noob java user here
    By New Guy_5 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 14th, 2011, 09:44 PM
  4. [SOLVED] Java Noob, Jframe in Applet.
    By rLLZORS in forum AWT / Java Swing
    Replies: 2
    Last Post: May 5th, 2011, 10:42 AM
  5. Need Help with my hmwk! Java noob!
    By ravij in forum Loops & Control Statements
    Replies: 4
    Last Post: October 7th, 2009, 01:02 AM

Tags for this Thread