Java 输出不同的行号以供每个输出使用。

huangapple go评论74阅读模式
英文:

Java outputting different row numbers for each output

问题

我有这段代码,它会打印出我拥有的每一行的最大值。但是,我也遇到了打印出每个输出的行号的问题。我尝试使用变量 i 来为每一行加 1。显然,这个方法不起作用,只是想展示一下我的尝试。如果没有 i,该代码可以正常工作,用于查找文本文件中字符串的最大值。如果有其他方法可以更改每个输出的行号,将不胜感激。例如:

Row 1: 5
Row 2: 67
Row 3: 43

这是我想要的。我已经尝试过的只有:

Row 1: 5
Row 1: 67
Row 1: 43

我的 Java 代码如下:

import java.util.Scanner;
import java.io.File;

public class maxValue {

    public static void main(String[] args) throws Exception {
        int i = 0;
        String fileName = "C:\\Users\\Michael\\Documents\\input.csv";
        File f = new File(fileName);
        Scanner fileScan = new Scanner(f);
        while (fileScan.hasNext()) {
            String line = fileScan.nextLine();
            i++; // 增加行号
            System.out.println("Row " + i + ": " + (extractMaximum(line)));

        }

        fileScan.close();
    }

    static int extractMaximum(String str) {
        int num = 0;
        int res = 0;

        // 开始遍历给定的字符串
        for (int i = 0; i < str.length(); i++) {
            // 如果出现数字,开始将其转换为整数,直到出现非数字字符
            if (Character.isDigit(str.charAt(i)))
                num = num * 10 + (str.charAt(i) - '0');

            // 更新最大值
            else {
                res = Math.max(res, num);

                // 重置数字
                num = 0;
            }
        }

        // 返回最大值
        return Math.max(res, num);
    }

}

现在你的代码应该能够正确打印出每一行的最大值,并且带有正确的行号。

英文:

I have this code that prints out the max value of each row I have. But I am having trouble also printing out the row number for each output.I tried using i to add 1 to each row. Obviously this does not work with it, just wanted to show my attempt. Without the i the code works fine for finding the max value of my string from a text file. If there is another way to change my row number for each output it would be much appreciated. For example,
<pre>
Row 1: 5
Row 2: 67
row 3: 43
</pre>
is what i want. All i have been able to get is:
<pre>
Row 1: 5
Row 1: 67
row 1: 43
</pre>

My java code

import java.util.Scanner;
public class maxValue {

	public static void main(String[] args) throws Exception {
            int i=0;i&lt;3;i++;
		    String fileName=&quot;C:\\Users\\Michael\\Documents\\input.csv&quot;;
		    File f = new File(fileName);
		    Scanner fileScan= new Scanner(f);
		    while(fileScan.hasNext()) {
		    	 String line=fileScan.nextLine();
		    	 System.out.println(&quot;ROW &quot; + i + &quot;: &quot; + (extractMaximum(line)));
		    	 
		    	
		    }
		    
		    fileScan.close();
		   

	}
	 static int extractMaximum(String str) { 
	        int num = 0; 
	        int res = 0; 
	       
	        // Start traversing the given string 
	        for (int i = 0; i&lt;str.length(); i++) { 
	            // If a numeric value comes, start converting 
	            // it into an integer till there are consecutive 
	            // numeric digits 
	            if (Character.isDigit(str.charAt(i))) 
	                num = num * 10 + (str.charAt(i)-&#39;0&#39;); 
	       
	            // Update maximum value 
	            else { 
	                res = Math.max(res, num); 
	       
	                // Reset the number 
	                num = 0; 
	            } 
	        } 
	       
	        // Return maximum value 
	        return Math.max(res, num); 
	    } 

}

答案1

得分: 1

你需要在循环中增加 i 的值:

public static void main(String[] args) throws Exception
{
    String fileName = "C:\\Users\\Michael\\Documents\\input.csv";
    File f = new File(fileName);
    Scanner fileScan = new Scanner(f);

    // 一个选项:使用 for 循环
    for (int i = 1; fileScan.hasNext(); i++) {
        String line = fileScan.nextLine();
        // 我更喜欢使用 printf
        System.out.printf("ROW %d: %d%n", i, extractMaximum(line));
    }
    fileScan.close();
}

// 重写以使用流
// 即使你不想使用流,
// 使用正则表达式拆分字符串也是一种改进
static int extractMaximum(String str)
{
     return Arrays.stream(str.split("\\D+"))
					.map(x -> Integer.parseInt(x))
					.max(Integer::compare)
					.get();
}
英文:

You need to increment i in the loop:

public static void main(String[] args) throws Exception
{
    String fileName=&quot;C:\\Users\\Michael\\Documents\\input.csv&quot;;
    File f = new File(fileName);
    Scanner fileScan = new Scanner(f);

    // One option: a for loop
    for (int i = 1; fileScan.hasNext(); i++) {
        String line = fileScan.nextLine();
        // I prefer printf
        System.out.printf(&quot;ROW %d: %d%n&quot;, i, extractMaximum(line));
    }
    fileScan.close();
}

// Re-written to use streams
// Even if you don&#39;t want to use stream,
// Using the regex to split the string is an improvement
static int extractMaximum(String str)
{
     return Arrays.stream(str.split(&quot;\\D+&quot;))
					.map(x -&gt; Integer.parseInt(x))
					.max(Integer::compare)
					.get();
}

</details>



huangapple
  • 本文由 发表于 2020年8月7日 02:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63289537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定