-6

I'm trying to write a program that gets a .txt file that only has something like 10000010000010000010001

I'm trying to count the number of zeros and output it like 5 5 5 3. I thought if I convert a string into a double or int I could write an if or for loop.

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ここでは、を使用して操作するのではなく、値の配列を使用することをお勧めします。doubleこれは、char配列にはさらに多くの値を格納できるためです。投稿した例は、double処理するには長すぎます。

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 に答える