Results 1 to 7 of 7
  1. #1
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Output is not what I expected.

    This program simply adds integers to an ArrayList and then prompt the user if they would like to delete specific numbers that they don't want.

    The output that I get when I want to delete numbers like 2 and 4 from 1 2 3 4 5 is 1 2 3 4 5 instead of 1 3 5.

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class AL 
    {
    	// A regular array like int arr[] has to have its size declare before run-time and it's not dynamic which mean it can not grow or expand on its own.
    	static Scanner input = new Scanner(System.in);
    	// You have to use reference types(Integer, Double, Float); not primitive types(int).
    	// Also, since these object are outside our main method, we want them to be static so that only have to be called once throughout the program and also so that values don't change and remain "static" throughout the program.
    	static ArrayList<Integer> array = new ArrayList<Integer>();
    	
    	public static void main(String[] args) 
    	{
    		System.out.println("Please enter an array of numbers. Type 0 when finished.");
    		int in = input.nextInt();
    		
    		while(in != 0) // Keep reading integers until the user enters zero.
    		{
    			array.add(in);
    			in = input.nextInt(); // read in the next integer.
    		}
    		
    		System.out.println("");
    		
    		printArray();
    		
    		System.out.println("\nWhat number would you like to delete?");
    		int del = input.nextInt();
    		
    		for(int i=0; i < array.size(); i++)
    		{
    			if(array.get(i) == del)
    			{
    				// Once The program has found the number the user is looking for, it will remove it from the List. 
    				// If the program doesn't find one, it will ask the user to enter another number until it reaches the end of the array.
    				array.remove(del);
    			}
    			else if(del == 0)
    			{
                                    // If the user is finished deleting numbers, then they must simply enter zero to quit entering numbers.
    				break; 
    			}		
    			
    			System.out.println("Enter another number.");
    			del = input.nextInt();
    		}
    		
    		System.out.println("");
    		
    		printArray();
    	}
    	
    	public static void printArray()
    	{
    		//If the user types 0.
    		System.out.println("--------------------------------");
    		
    		for(int x : array) 
    		{
    		   System.out.println(x);	
    		}
    	}
    }
    Last edited by Deathslice; 01-21-2015 at 06:17 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Output is not what I expected.

    If you do 'array.remove(3)' not an object '3' will be removed, but the object at index location 3 will be removed ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Output is not what I expected.

    Thanks, I changed my for loop to this:

    *Corrected Version.

    Java Code:
    for(int i=0; i < array.size()-1; i++)
    {
    if(array.contains(del))
    {
    // Once The program has found the number the user is looking for, it will remove it from the List. 
    // If the program doesn't find one, it will ask the user to enter another number until it reaches the end of the array.
    array.remove(del-1);
    }
    else if(del == 0)
    {
    break; // If the user is finished deleting numbers, then they must simply enter zero to quit entering numbers.
    }	
    
    System.out.println("Enter another number.");
    del = input.nextInt();
    }
    Even though I get an exception if enter a number outside of the array, this is something which I already know. The problem now is if I enter 1 2 3 4 5 and I enter 4, it removes 5 and not 4. Any ideas?
    Last edited by Deathslice; 01-21-2015 at 09:49 PM.

  4. #4
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Output is not what I expected.

    Never mind, I fixed it. All I had to was put a minus one after remove del because that method works by index. So, if I had entered 5, it would go out of bounds because the program is counting from zero to four. The correct version is on my second post.

  5. #5
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Output is not what I expected.

    So it turns that I did not fix it properly. I then decided to mess around with it a little more and I think I came up with a good solution

    Java Code:
    System.out.println("\nWhat number would you like to delete?");
    
    while(array.size() > 0)
    {
    int del = input.nextInt();
    
    int index = array.indexOf(del);
    
    if(array.contains(del))
    {
    // Once The program has found the number the user is looking for, it will remove it from the List. 
    // If the program doesn't find one, it will ask the user to enter another number until it reaches the end of the array.
    array.remove(index);
    }
    else if(del == 0)
    {
    break; // If the user is finished deleting numbers, then they must simply enter zero to quit entering numbers.
    }	
    
    System.out.println("Enter another number that you would like to delete. Else, enter zero");
    }
    The problem is with the while loop. Even if the array size is zero, it still keeps on going(I also made sure that the array size was in fact zero and it was so I don't know what is going on there).

    Update: Never mind I fixed it. I made a really stupid mistake(>= 0). Haha
    Last edited by Deathslice; 01-21-2015 at 09:50 PM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Output is not what I expected.

    In the future, please place code tags [code][/code]around your code to properly format it. This is easily done in the advanced editor.

    Click the go advanced tab.
    Highlight your text.
    Then click the # sign.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Output is not what I expected.

    Thanks, I've made the corrections and properly placed my code inside the code tags.

Similar Threads

  1. Replies: 4
    Last Post: 12-13-2013, 03:16 AM
  2. Replies: 5
    Last Post: 06-25-2012, 01:02 PM
  3. Replies: 3
    Last Post: 04-19-2011, 07:21 PM
  4. output not as expected.What's wrong?
    By abdullahansari in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 09:01 PM
  5. output not what expected!
    By aza101 in forum Java Applets
    Replies: 0
    Last Post: 07-10-2009, 07:17 AM

Tags for this Thread

Posting Permissions

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