计算零的数量

时间:2012-12-13 06:04:11

标签: java

我正在尝试编写一个获取.txt文件的程序,该文件只有10000010000010000010001

我正在尝试计算零的数量并将其输出为5 5 5 3.我想如果我将字符串转换为doubleint我可以写一个{{1} }或if循环。

for

3 个答案:

答案 0 :(得分:2)

你走了:

char[] numArray = num.toCharArray();
  int counter=0;
  for(int i=0;i<numArray.length;i++) {
     if(numArray[i]=='0') {
        counter++; 
     }
     if((i==numArray.length-1&&counter>0)||(counter>0&&numArray[i]!='0')) {
        System.out.println("Number of Zeroes: "+counter);
        counter=0;
     }
  }

一些要点:

1)最好在此处使用char值数组,而不是使用double进行操作,因为char数组可以存储更多值 - 您发布的示例太长,double无法处理。

2)大部分内容应该是不言自明的(至少,如果你逐个研究它),但是如果i==numArray.length-1部分令人困惑,这可以确保如果字符串以a结尾0,最终计数0也将被打印出来。

这应该适用于你可以抛出的任何字符串 - 包括0和1之外的值,如果你需要支持它的话!

答案 1 :(得分:1)

你的努力在哪里? 你可以简单地尝试(如果你的字符串只包含1和0):

    String[] splitArr = num.split("1");
    String countStr = "";
    for (int i = 0; i < splitArr.length; i++) {
        if( ! splitArr[i].isEmpty() )
            countStr += splitArr[i].length();
    }
    System.out.println(countStr);

答案 2 :(得分:0)

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

public class ZeroCounter {

ArrayList <Integer> listOfNumbers = new ArrayList <Integer> ();
DataInputStream inStream;

long inFileSize;
long outFileSize;

// Track how many bytes we've read. Useful for large files.
int byteCount;

    public ZeroCounter() {
    }

//read the file and turn it into an array of integers
public void readFile(String fileName) {

    try {
        // Create a new File object, get size
        File inputFile = new File(fileName);
        inFileSize = inputFile.length();

        // The constructor of DataInputStream requires an InputStream
        inStream = new DataInputStream(new FileInputStream(inputFile));
    }

    // Oops.  Errors.
    catch (FileNotFoundException e) {
        e.printStackTrace();
        System.exit(0);
    }

    // Read the input file
    try {

        // While there are more bytes available to read...
        while (inStream.available() > 0) {

            // Read in a single byte and store it in a character
            int c = (int)inStream.readByte();

            if ((++byteCount)% 1024 == 0)
                System.out.println("Read " + byteCount/1024 + " of " + inFileSize/1024 + " KB...");

            // Print the integer to see them for debugging purposes
            //System.out.print(c);
            // Add the integer to an ArrayList
            fileArray.add(c);
        }

        // clean up
        inStream.close();
        System.out.println("File has been converted into an ArrayList of Integers!");
    }

    // Oops.  Errors.
    catch (IOException e) {
        e.printStackTrace();
        System.exit(0);
    }

    //Print the ArrayList contents for debugging purposes
    //System.out.println(fileArray);
}


public void countZeroes() {
    int zeroCounter = 0;
    for (int i = 0; i < listOfNumbers.size(); i++) {
        if (listOfNumbers.get(i) == 0) {
            zeroCounter++;
        }
        else if (listOfNumbers.get(i) != 0 && zeroCounter > 0) {
            //this only prints the number of zeroes if the zero counter isn't zero
            System.out.println(zeroCounter + " ");
            zeroCounter = 0;
        }

        else {
            //do nothing
        }
    }
}

public static void main(String[] args) {
        ZeroCounter comp = new ZeroCounter();
        comp.readFile("test3.txt");
        comp.countZeroes();
    }
}