13 Replies - 3717 Views - Last Post: 04 March 2012 - 02:04 PM Rate Topic: -----

#1 CoCo5678   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 12

compact.java problem is it cant take out zeros

Posted 03 March 2012 - 02:24 PM

Hi! I am making a program called compact.java for my computer programming class and the task is to Assignment:

Write a program that reads a text file (compact.txt) and stores the integers in an array. Your instructor will provide this text file.

Write a method compact that removes all zeroes from the array, leaving the order of the other elements unchanged. All local variables within this function must be scalar. In other words, you may not use a second array to solve the problem.

Do not solve the problem by printing out only the non-zero values in the array. The compact method must remove all zeros from the array.

Instructions:

Print out the list both before and after removing the zeros. For example:

Before: 0, 9, 7, 0, 0, 23, 4, 0

After: 9, 7, 23, 4

Your program must use proper modular design and parameter passing.

I made my program and I think I made it right, however it is only printing out the before line and not the after line where there are no zeros! My code is displayed below and my compact.txt file reads: 0 6 13 0 0 75 33 0 0 0 4 29 21 0 86 0 32 66 0 0. Could you please tell me where I am going wrong? Thank you for any help you can provide!
package chap;

import java.util.Scanner;	
import java.io.*;

	public class Compact{

		public void printList(int[] list){
			for(int index = 0; index < list.length; index++){
				System.out.print(list[index]+"  ");
			}
			System.out.println();
		}
		
		public int[] getArray(){
			int value;
			int count = 0;
			int[] scannedArray = new int[getListSize()]; 
			Scanner in;
			try{
				in = new Scanner(new File("Compact.txt"));	
				while(in.hasNextInt()){
					value = in.nextInt();
					scannedArray[count] = value;
					count++;
				}
				}
				
			catch(IOException z){
				System.out.println("File Not Found");
			}

			return scannedArray;
		}
		
		public static int getListSize(){
			int count = 0;
			Scanner readText;
				   try{
						   readText = new Scanner(new File("Compact.txt"));
						   while(readText.hasNextInt()){
								count++;
								readText.nextInt();
							}
				   }
			catch(IOException z){
				System.out.println("File not found");
			}
			return count;
		}
		
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Compact startingArray = new Compact();
		startingArray.printList(startingArray.getArray());
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: compact.java problem is it cant take out zeros

#2 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: compact.java problem is it cant take out zeros

Posted 03 March 2012 - 02:33 PM

Your code is OK up to now
Now you need a method that removes the 0 from the array using probably two imbricated loop

int nonZeroSize = 0;
for(int i = 0; i < list.length; ++i) {
   if(list[i] == 0) {
      for(.... move up all the following list element by one
      ++nonZeroSize;


Was This Post Helpful? 1
  • +
  • -

#3 CoCo5678   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 12

Re: compact.java problem is it cant take out zeros

Posted 03 March 2012 - 02:37 PM

// Shift items up by one position
for (int i = targetIndex; i < logicalSize - 1; i++)
array[i] = array[i + 1];


would something like that ^ work?
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: compact.java problem is it cant take out zeros

Posted 03 March 2012 - 02:46 PM

int nonZeroSize = 0;
for(int i = 0; i < list.length; ++i) {
   if(list[i] == 0) {
      ++nonZeroSize;
      for(int j = i; j < list.length - nonZeroSize; ++j)
          list[j] = list[j+1];
   }
}
// now print the array without 0
for(int i = 0; i < nonZroSize; ++i)
  System.out.print(" " + list[i]);


Was This Post Helpful? 1
  • +
  • -

#5 CoCo5678   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 12

Re: compact.java problem is it cant take out zeros

Posted 03 March 2012 - 02:54 PM

View Postpbl, on 03 March 2012 - 02:46 PM, said:

int nonZeroSize = 0;
for(int i = 0; i < list.length; ++i) {
   if(list[i] == 0) {
      ++nonZeroSize;
      for(int j = i; j < list.length - nonZeroSize; ++j)
          list[j] = list[j+1];
   }
}
// now print the array without 0
for(int i = 0; i < nonZroSize; ++i)
  System.out.print(" " + list[i]);



thank you so much! Where would I put that in my code? Like would i make a new method or add that to my getArray() method?
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: compact.java problem is it cant take out zeros

Posted 03 March 2012 - 02:58 PM

Really your choice...
I would put that code just after the
System.out.println(); of your printList() method
Was This Post Helpful? 1
  • +
  • -

#7 CoCo5678   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 12

Re: compact.java problem is it cant take out zeros

Posted 03 March 2012 - 03:02 PM

I inserted that method in and it only got rid of some of the zeros! the output looked like this:
0  6  13  0  0  75  33  0  0  0  4  29  21  0  86  0  32  66  0  0  

6  13  0  75  33  0  4  29  21  86  32  66  0  0  


Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: compact.java problem is it cant take out zeros

Posted 03 March 2012 - 03:33 PM

Can't be done. You can only remove zeros by creating a new array. All you can do is move them
Was This Post Helpful? 0
  • +
  • -

#9 Sheph   User is offline

  • D.I.C Lover
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,032
  • Joined: 12-October 11

Re: compact.java problem is it cant take out zeros

Posted 03 March 2012 - 04:07 PM

Add an i-- after you find a 0 and shift everything down. Because you haven't checked the next variable yet and that is in the current i index, so it will be skipped when i gets incremented.
int nonZeroSize = 0;
for(int i = 0; i < list.length; ++i) {
   if(list[i] == 0) {
      ++nonZeroSize;
      for(int j = i; j < list.length - nonZeroSize; ++j)
          list[j] = list[j+1];
      i--; // Decrement i to check the value that was shifted
   }
}
// now print the array without 0
for(int i = 0; i < nonZeroSize; ++i)
  System.out.print(" " + list[i]);

Was This Post Helpful? 1
  • +
  • -

#10 CoCo5678   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 12

Re: compact.java problem is it cant take out zeros

Posted 04 March 2012 - 12:59 PM

View PostSheph, on 03 March 2012 - 04:07 PM, said:

Add an i-- after you find a 0 and shift everything down. Because you haven't checked the next variable yet and that is in the current i index, so it will be skipped when i gets incremented.
int nonZeroSize = 0;
for(int i = 0; i < list.length; ++i) {
   if(list[i] == 0) {
      ++nonZeroSize;
      for(int j = i; j < list.length - nonZeroSize; ++j)
          list[j] = list[j+1];
      i--; // Decrement i to check the value that was shifted
   }
}
// now print the array without 0
for(int i = 0; i < nonZeroSize; ++i)
  System.out.print(" " + list[i]);


i tried doing that but it only caused an out of bounds exception error! and it wouldn't print the second line not even with the zero's still in it
Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: compact.java problem is it cant take out zeros

Posted 04 March 2012 - 01:20 PM

Yes... nice bug :)
Will happen if the last element in the list is a 0 because it will push up and up and up and will be retested
this should do it

		int nonZeroSize = 0;
		int last = list.length;              // last element in the list
		for(int i = 0; i < last - 1; ++i) {  // to the last - 1
			if(list[i] == 0) {
				++nonZeroSize;
				-- last;             // update last
				for(int j = i; j < last; ++j) {
					list[j] = list[j+1];
				}
				i--; // Decrement i to check the value that was shifted
			}
		}
		// now print the array without 0
		for(int i = 0; i < nonZeroSize; ++i)
			System.out.print(" " + list[i]);


Was This Post Helpful? 1
  • +
  • -

#12 CoCo5678   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 12

Re: compact.java problem is it cant take out zeros

Posted 04 March 2012 - 01:42 PM

View Postpbl, on 04 March 2012 - 01:20 PM, said:

Yes... nice bug :)
Will happen if the last element in the list is a 0 because it will push up and up and up and will be retested
this should do it

		int nonZeroSize = 0;
		int last = list.length;              // last element in the list
		for(int i = 0; i < last - 1; ++i) {  // to the last - 1
			if(list[i] == 0) {
				++nonZeroSize;
				-- last;             // update last
				for(int j = i; j < last; ++j) {
					list[j] = list[j+1];
				}
				i--; // Decrement i to check the value that was shifted
			}
		}
		// now print the array without 0
		for(int i = 0; i < nonZeroSize; ++i)
			System.out.print(" " + list[i]);



yes!! you are brilliant! That solved that problem but it created one last problem... it is missing the last number now it looks like this:
0  6  13  0  0  75  33  0  0  0  4  29  21  0  86  0  32  66  0  0  

6  13  75  33  4  29  21  86  32  



i am missing the last 66!
Was This Post Helpful? 0
  • +
  • -

#13 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: compact.java problem is it cant take out zeros

Posted 04 March 2012 - 01:52 PM

Actually the ++nbNonZero is at the wrong place. It will print the first nbZero in the list
To make it work if the last one is a 0 or not should be:

		int nonZeroSize = 0;
		int last = list.length;
		for(int i = 0; i < last; ++i) {
			if(list[i] == 0) {
				-- last;
				for(int j = i; j < last; ++j) {
					list[j] = list[j+1];
				}
				i--; // Decrement i to check the value that was shifted
			}
			else {
				++nonZeroSize;				
			}
		}
		// now print the array without 0
		for(int i = 0; i < nonZeroSize; ++i)
			System.out.print(" " + list[i]);
	}


My apologies
Was This Post Helpful? 1
  • +
  • -

#14 CoCo5678   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 12

Re: compact.java problem is it cant take out zeros

Posted 04 March 2012 - 02:04 PM

View Postpbl, on 04 March 2012 - 01:52 PM, said:

Actually the ++nbNonZero is at the wrong place. It will print the first nbZero in the list
To make it work if the last one is a 0 or not should be:

		int nonZeroSize = 0;
		int last = list.length;
		for(int i = 0; i < last; ++i) {
			if(list[i] == 0) {
				-- last;
				for(int j = i; j < last; ++j) {
					list[j] = list[j+1];
				}
				i--; // Decrement i to check the value that was shifted
			}
			else {
				++nonZeroSize;				
			}
		}
		// now print the array without 0
		for(int i = 0; i < nonZeroSize; ++i)
			System.out.print(" " + list[i]);
	}


My apologies


Now it works perfectly! Thank you all for all of your help! You guys rocked and I learned alot! Thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1