Results 1 to 18 of 18

Thread: ArrayList<>

  1. #1
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default ArrayList<>

    I need to create a program that uses ArrayList to store integers the user inputs, then scan the array for the largest number. I would also like the user to be able to exit this loop if the number 0 is entered.

    As you can see below, I'm not sure how to correctly exit the do-while loop. I found this on another forum, but it does not work.

    Java Code:
    import java.util.*;
    
    public class array {
    	public static void main(String [] args){
    		ArrayList<Integer> list = new ArrayList<Integer>();
    		Scanner input = new Scanner(System.in);
    		do{
    		System.out.print("Enter number: ");
    		list.add(input.nextInt());}
    		while(list.equals(0));
    		
    		Collections.sort(list);
    		System.out.print(max(list));
    		System.out.println(list.get(list.size()));
    		}
    
    	private static Integer max(ArrayList<Integer> list) {
    		return null;
    	}	
    }
    Last edited by javaStooge; 03-24-2014 at 08:52 PM.

  2. #2
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ArrayList<>

    Part of this code requires that I scan the list and display the largest integer, in addition to that, if the list is null or 0, it must return null.

    I'm not sure how to set the ArrayList as a parameter of the if-else statement:

    Java Code:
    private static Integer max(ArrayList<Integer> list) {
    		if(ArrayList<Integer> list > 0){
    			System.out.println(list.get(list.size()));
    		}
    		else
    		return null;
    	}

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: ArrayList<>

    I presume that if the list reference is null or the list size is 0 you must return null (guess which one you should check for first). As far as iterating over a list,
    the easiest way is to use the modified for loop. Or you can use a regular for loop and list.get().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ArrayList<>

    What about exiting the do-while loop? Is it not possible to exit the loop from a user input?

    If I use the list.get() method, how do I retrieve the largest integer? I thought that was what I kind of had going on there in my second code.
    Last edited by javaStooge; 03-24-2014 at 09:25 PM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: ArrayList<>

    To find the largest integer, you need to iterate over the loop and update your initial guess at max to anything which is greater than the current max you have found. And yes,you can exit a do while loop based on user input. Just check the value you entered for the proper condition of the while expression.

    Java Code:
    do {
     // do something. exit if someone enters 23.
    } while (input != 23);
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: ArrayList<>

    Hi,

    I am just wondering how i could help you.

    - When you try to write some logic in Java, you should read about the constructs that you are using. So if your code does not compile: check the error and fix it. Read the reference of an ArrayList if you do not know how to work with it.

    - When you want to write some logic, then just take a piece of paper and play around and do exactly what should happen in code. That way you get some pseudo code that could be translated to Java. So for your input logic, you could have something like

    a) Ask user for a number. (You write the number to a location on your paper so you need a variable. That easy!)
    b) If the number is not 0, then you store the number in your array and then you continue at a)
    c) if the number is 0, you simply proceed.

    Same with checking for the highest number:
    a) You need a place on your paper where you want the highest number.
    b) You put the first element of your array in there.
    c) You check the next element if it is greater, If that is the case you override the highest number.
    d) If more elements are available you repeat at c)

    I hope this helped a little bit. And this is not just a beginner thing. Even experts do such stuff (but not with this detail but for our development team all the design meetings cannot be done without a whiteboard on which we think about the complex design stuff!)

    With kind regards,

    Konrad

  7. #7
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ArrayList<>

    My thought was that if I create an if statement that scans the loop for the integers greater than 0, then use the get method to retrieve the largest integer. When I do this though, there is an error when I set the parameter as shown above.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default Re: ArrayList<>

    there is an error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: ArrayList<>

    Hi,

    you should read about the ArrayList. Your "list.equals(..)" is comparing the List object which simply does not make sense.
    What do you want to do exactly? You do not want to check the array - you want to check the last inserted number instead ....
    And do you really want the 0 to be included inside the array? It is the "stop signal" and not a number that should be compared.

    And inside the max function:
    a) you should modify the if statement. It is simply not in a correct syntax, You want to check if the list is null or empty or not. So how to check if a reference is null? And maybe you have a closer look at ArrayList so you could find out how to check if it is empty?
    b) And if you want to get the last item, you have to consider that the first item has index 0, the second has index 1 and so on ..

    And if you have to implement a function that gets the highest number of an array, I would be surprised if sorting the array using some build in functions is something that a teacher wants to see. I think you should learn how to go through an array to find the highest number. But that is just my guess. Your solution should work with these changes.

    With kind regards,

    Konrad

  10. #10
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ArrayList<>

    Hey, thank you all for the guidance in resolving what is probably a very simple solution.

    This is what I've decided on thus far, however I'm not sure I know how to call the max method within in main. I'm hoping that is why I am getting this output in the console:

    ArrayList&lt;&gt;-screen-shot-2014-03-24-6.24.41-pm.jpg


    Java Code:
    public class array {
    	public static void main(String [] args){
    		ArrayList<Integer> list = new ArrayList<Integer>();
    		Scanner input = new Scanner(System.in);
    		do{
    			System.out.print("Enter number: ");
    			list.add(input.nextInt());
    			}
    		while(input.nextInt() != 1);
    	}
    		
    	private static Integer max(ArrayList<Integer> list) {
    		int maxValue = Collections.max(list);
    		if(maxValue > 0){
    			return maxValue;
    		}
    		else
    		return null;
    	}	
    }

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: ArrayList<>

    Quote Originally Posted by javaStooge View Post
    Part of this code requires that I scan the list and display the largest integer, in addition to that, if the list is null or 0, it must return null.
    It's usually a bad idea to return null when the arguments don't make sense. It would be far better to throw a NullPointerException if the list is null and an IllegalArgumentException if it's empty.
    Get in the habit of using standard Java naming conventions!

  12. #12
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ArrayList<>

    When I run this program, I have to enter the number twice, that is what was going on in the image above. The first number I enter does not take, but the second time it does. Why would that be?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default Re: ArrayList<>

    How many times is nextInt() called? What happens to the value that is read each time? Are all the values used properly?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ArrayList<>

    Java Code:
    public static void main(String [] args){
    		ArrayList<Integer> list = new ArrayList<Integer>();
    		Scanner input = new Scanner(System.in);
    		do{
    			System.out.println("Enter number that ends with 0 (1 to exit): ");
    			list.add(input.nextInt());
    			}
    		while(input.nextInt() != 1);
    It was mentioned that in order to exit the do-while loop, I would need to use (input != 0), except that when I did that, it erred out with "incompatible operand types Scanner and int". Instead, I used the code above and there was no error. I think its safe to assume that this is the issue.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default Re: ArrayList<>

    Did you see my questions? Look at the code and try to answer them:
    1)How many times is nextInt() called?
    2)What happens to the value that is read each time?
    3)Are all the values used properly?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: ArrayList<>

    Hi,

    I think this is a good example of bad naming. What does your variable "input" contain? Not the input of the user. So the name is simply misleading. Rename the variable to something that makes sense. You could simply name it scanner because it contains the scanner for the user input. Or if you want to use longer names something like userInputScanner could be nice. But if there is just one scanner and nothing else like that, the name scanner could be ok. (This could be required if you have multiple scanner objects e.g. from reading data of a file or maybe some other Scanner classes with instances of that ...)

    And I agree to Norm: Please check his questions. Then you should see the problem. It is important in my eyes that you see what is going on in your code. Without that, you cannot work on that code at all! And maybe you directly find the solution with my comment about input - if there is no input variable now, then you might see, what is required ...

    With kind regards,

    Konrad

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,894
    Rep Power
    5

    Default Re: ArrayList<>

    I think input is just a fine name. The code reads nicely because of it. Just look at line 6 of the above code, if you read it from right to left it tells the whole store.

    'Read the next integer from the input and add it to the list'. Its poetry I tell you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: ArrayList<>

    Hi gimbal2,

    that shows that the preference can be different from person to person. Everyone has to decide what to use or do. And in this case, in my eyes input is the number entered and not the scanner on the stdin stream.
    But I see your point.

    I am tempted to show the code that I would prefer but I am not posting it here right now so Norms question can still lead javaSpooge. Instead I sent you a quick PM on this. (I will provide it later to this thread of course.)

    With kind regards,

    Konrad

Similar Threads

  1. Replies: 3
    Last Post: 10-11-2013, 05:18 PM
  2. Replies: 0
    Last Post: 09-20-2013, 11:28 PM
  3. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

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