该程序假设找到该类的名称,并检查该类的第一个char是否为大写且类似于方法和常量。 以下是我的代码:
public class CamelCase {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new File("Input.txt"));
PrintWriter output = new PrintWriter("Output.txt");
while (scanner.hasNextLine()) {
String line = scanner.next();
count++;
if (line.contains("class")) {
line = scanner.nextLine();
if (Character.isLowerCase(line.charAt(0))) {
System.out.println("dd" + count);
}
} else if (line.contains("void")) {
line = scanner.next();
if (Character.isUpperCase(line.charAt(0))) {
System.out.println("Error at line " + count);
}
} else if (line.contains("final")) {
line = scanner.next();
if (line.contains("int") || line.contains("double")
|| line.contains("String")) {
line = scanner.next();
if (!(line.equals(line.toUpperCase()))) {
System.out.println("Needs to be uppercase");
}
}
}
}
}
}
有没有办法使用scanner.next();
计算行号?
谢谢!
答案 0 :(得分:0)
愿这是您的解决方案:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class solution {
public static int count = 0; // a static variable for counting
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("C:\\Users\\Himel Rana\\Desktop\\test.txt"));
while(sc.hasNextLine()) {
count ++; // counter incrementing every time by 1
// you have to do something here
// I am printing line
System.out.println(sc.nextLine());
}
// result outside of your while loop
System.out.println("Total Line: "+count);
}
}