-6

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

我正在尝试计算零的数量并将其输出为 5 5 5 3。我想如果我将字符串转换为 adouble或者int我可以编写一个iforfor循环。

import java.util.Scanner;

public class test1{

    public static void main(String[] args) {

        java.io.File test2 = new java.io.File("test3.txt");

        try
        {
           Scanner input = new Scanner(test2);

        while(input.hasNext())
        {
            String num = input.nextLine();
            System.out.println(num);

            double n = Double.parseDouble(num);
            System.out.println(n);
        } 
        }

        catch (Exception e){
            System.out.println("could not find file");
        }   

       }

}
4

3 回答 3

2

你的努力在哪里?您可以简单地尝试(如果您的字符串仅包含 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);
于 2012-12-13T06:13:19.047 回答
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此处使用值数组,而不是使用 a 操作double,因为char数组可以存储更多值-您发布的示例太长,double无法处理 a 。

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

这应该适用于您可以扔给它的任何字符串 - 如果您需要支持它,包括 0 和 1 以外的值!

于 2012-12-13T06:13:32.090 回答
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();
    }
}
于 2012-12-13T06:56:50.040 回答