12 Replies - 1006 Views - Last Post: 15 October 2012 - 09:20 AM Rate Topic: -----

#1 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

ArrayList Java

Posted 14 October 2012 - 09:52 PM

I have the following code, it works perfectly fine except for the fact when it prints the results it includes the word i use to "terminate" the string. My question is, how would i remove that or would there be a way to end the program after entering 10 numbers? Also, is this code a good code or would there be another way of going about it that would be simpler?

import java.util.ArrayList;
import java.util.Scanner;
public class ReverseOrderArrayList 
{
public static void main (String[] args)
{
		ArrayList<String> inputs = new ArrayList<String>();
		Scanner scan = new Scanner (System.in);
		int inputCount = -1;
		String newInput = "";
		String terminate = "Finished";
		
		System.out.println ("Enter Your Numbers");
		System.out.println ("Enter 'Finshed' when done. ");

		
		do
		{
		newInput = scan.next();
		inputs.add(newInput);
		inputCount ++;
		}
		while (!newInput.equals(terminate));
		System.out.println(inputs);
		
		System.out.print("[");
		do
		{
		System.out.print(inputs.get(inputCount) + ", ");
		inputCount --;
		}
		while (inputCount > 0);
		System.out.print(inputs.get(0) + "]");
		
}

}



This is the result and as you can see it prints out finished along with the list.
Enter Your Numbers
Enter 'Finshed' when done. 
1
2
3
4
Finished
[1, 2, 3, 4, Finished]
[Finished, 4, 3, 2, 1]



Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList Java

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: ArrayList Java

Posted 14 October 2012 - 10:11 PM

Your whole printing thing is tricky, but it all boils down to you putting in the input to the ArrayList. Just make sure that if the word "Finished" is inputted, you don't add it to the ArrayList. Easily done with an if statement:
if(newInput.equals("Finished")==false){
//add to ArrayList
}
//or
if(!newInput.equals("Finished")){
//add to ArrayList
}



That's it. ^^
Was This Post Helpful? 1
  • +
  • -

#3 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: ArrayList Java

Posted 14 October 2012 - 11:39 PM

View PostfromTheSprawl, on 14 October 2012 - 10:11 PM, said:

Your whole printing thing is tricky, but it all boils down to you putting in the input to the ArrayList. Just make sure that if the word "Finished" is inputted, you don't add it to the ArrayList. Easily done with an if statement:
if(newInput.equals("Finished")==false){
//add to ArrayList
}
//or
if(!newInput.equals("Finished")){
//add to ArrayList
}



That's it. ^^


Now by doing this will it remove the "finished"? and where would insert this before the program runs or at the end when printing?
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: ArrayList Java

Posted 14 October 2012 - 11:44 PM

While you're getting the input and adding them to the ArrayList. Yes, it would remove the finished.
Was This Post Helpful? 0
  • +
  • -

#5 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: ArrayList Java

Posted 15 October 2012 - 08:15 AM

View PostfromTheSprawl, on 14 October 2012 - 11:44 PM, said:

While you're getting the input and adding them to the ArrayList. Yes, it would remove the finished.


I still seem to get finished in the end results even after i added the if statement.
Was This Post Helpful? 0
  • +
  • -

#6 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: ArrayList Java

Posted 15 October 2012 - 08:21 AM

View Postamanjot24, on 15 October 2012 - 08:15 AM, said:

View PostfromTheSprawl, on 14 October 2012 - 11:44 PM, said:

While you're getting the input and adding them to the ArrayList. Yes, it would remove the finished.


I still seem to get finished in the end results even after i added the if statement.


Do you mind posting the updated code?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: ArrayList Java

Posted 15 October 2012 - 08:25 AM

For that type of things, always easier to make an infinite loop and to break out of it

     while(true)     // infinite loop 
     {  
         newInput = scan.next();
         if(newInput.eqals(terminate))
            break;                     // done exit the loop  
         inputs.add(newInput);  
         inputCount ++;                // kind of useless can be obtained by inputs.size()  
     }   // end infinite loop  


Was This Post Helpful? 2
  • +
  • -

#8 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: ArrayList Java

Posted 15 October 2012 - 08:40 AM

View Postpbl, on 15 October 2012 - 08:25 AM, said:

For that type of things, always easier to make an infinite loop and to break out of it

     while(true)     // infinite loop 
     {  
         newInput = scan.next();
         if(newInput.eqals(terminate))
            break;                     // done exit the loop  
         inputs.add(newInput);  
         inputCount ++;                // kind of useless can be obtained by inputs.size()  
     }   // end infinite loop  



After changing some things around this my updated code and i still seem to be struggling with the ideas.

import java.util.ArrayList;
import java.util.*;
public class ReverseOrderArrayList 
{
public static void main (String[] args)
{
		ArrayList<String> inputs = new ArrayList<String>();
		Scanner scan = new Scanner (System.in);
		
		//Declare Variables
		int numbercount = -1;
		String number = "";
		String terminate = "Finished";
		
		System.out.println ("Enter Your Numbers");//Entering Numbers to List
		System.out.println ("Enter 'Finished' when done. ");//Ends Program
		
		//Reads the inputs 
		while(true);     
		{  
		    number = scan.next();
		    if(number.equals(terminate))
		       break;                    
		    inputs.add(number);  
		    inputs.size();	
		}    


		//Prints the original list	
		System.out.println(inputs);
		System.out.print("[");
		do
			//Prints the reverse order list 
		{
		System.out.print(inputs.get(numbercount) + ", ");
		numbercount --;
		}
		while (numbercount > 0);
		System.out.print(inputs.get(0) + "]");

		


		
}

}


Was This Post Helpful? 0
  • +
  • -

#9 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: ArrayList Java

Posted 15 October 2012 - 08:49 AM

You have a semi-colon after your while loop.
Was This Post Helpful? 0
  • +
  • -

#10 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: ArrayList Java

Posted 15 October 2012 - 08:52 AM

View PostKinaces, on 15 October 2012 - 08:49 AM, said:

You have a semi-colon after your while loop.


Thank you! but this way it only prints out the original list but not the reverse list, like i had earlier.
Was This Post Helpful? 0
  • +
  • -

#11 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: ArrayList Java

Posted 15 October 2012 - 09:09 AM

Your numberCount is -1, and you are subtracting to it. numberCount needs to be equal to the size of the array list.
Was This Post Helpful? 0
  • +
  • -

#12 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: ArrayList Java

Posted 15 October 2012 - 09:14 AM

View PostKinaces, on 15 October 2012 - 09:09 AM, said:

Your numberCount is -1, and you are subtracting to it. numberCount needs to be equal to the size of the array list.


After changing that this my program.

import java.util.ArrayList;
import java.util.*;
public class ReverseOrderArrayList 
{
public static void main (String[] args)
{
		ArrayList<String> inputs = new ArrayList<String>();
		Scanner scan = new Scanner (System.in);
		
		//Declare Variables
		int numbercount = inputs.size();
		String number = "";
		String terminate = "Finished";
		
		System.out.println ("Enter Your Numbers");//Entering Numbers to List
		System.out.println ("Enter 'Finished' when done. ");//Ends Program
		
		//Reads the inputs 

		while(true)   
		{  
		    number = scan.next();
		    if(number.equals(terminate))
		       break;                    
		    inputs.add(number);  
		    inputs.size();	
		}    

        while (!number.equals(terminate));
        System.out.println(inputs);

		//Prints the original list	
		System.out.println(inputs);
		System.out.print("[");
		do
			//Prints the reverse order list 
		{
		System.out.print(inputs.get(numbercount) + ", ");
		numbercount --;
		}
		while (numbercount > 0);
		System.out.print(inputs.get(0) + "]");

		


		
}

}



and this is my results

Enter Your Numbers
Enter 'Finished' when done. 
1
2
3
Finished
[1, 2, 3]
[1, 2, 3]
[1, 1]



still i do not get the reverse order of the array list
Was This Post Helpful? 0
  • +
  • -

#13 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: ArrayList Java

Posted 15 October 2012 - 09:20 AM

You set the numberCount before you even added anything to the ArrayList. So numberCount is still 0. Also since Java arrays are 0 indexed you have to set it to the size - 1.

You also have another semi-colon after another while loop
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1