java 计算零的数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13853921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
counting the number of zeros
提问by henry
I'm trying to write a program that gets a .txt file that only has something like 10000010000010000010001
我正在尝试编写一个获取 .txt 文件的程序,该文件只有 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.
我正在尝试计算零的数量并将其输出为 5 5 5 3。我想如果我将字符串转换为 adouble
或者int
我可以编写一个if
orfor
循环。
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");
}
}
}
回答by username tbd
Here you go:
干得好:
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;
}
}
Some important points:
一些要点:
1) It's best to use an array of char
values here, instead of operating using a double
, because a char
array can store many more values- the example you posted is too long for a double
to handle.
1) 最好在char
这里使用值数组,而不是使用 a 进行操作double
,因为char
数组可以存储更多值-您发布的示例太长了 adouble
无法处理。
2) Most of this should be self-explanatory (at least, if you study it bit-by-bit), but in case the i==numArray.length-1
part is confusing, this ensures that if the string ends with a 0, the final count of 0's will be printed out as well.
2)大部分应该是不言自明的(至少,如果你一点一点地研究它),但如果i==numArray.length-1
部分令人困惑,这可以确保如果字符串以 0 结尾,则 0 的最终计数将也可以打印出来。
This should work for any string you can throw at it- including values besides 0 and 1, if you need support for it!
这应该适用于您可以抛出的任何字符串 - 包括 0 和 1 之外的值,如果您需要支持的话!
回答by vishal_aim
where is your effort? you can simply try (if your string contains only 1s and 0s):
你的努力在哪里?您可以简单地尝试(如果您的字符串仅包含 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);
回答by hologram
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();
}
}