我正在尝试将行分配给大写字母和列到小字母,但第二个while语句由于某种原因不打印任何内容
仅供参考我尝试将行分配给数字,但无法将int分配给字符串。
Txt文件包含:*行之间没有空格
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
如果给定特定的行和列,是否可以更改元素?
提前谢谢你。
编辑:在主类我只调用这个类中的方法。
package archmap;
import java.io.*;
import java.util.*;
public class ReadFile {
private Scanner Reader;
Scanner Vari = new Scanner(System.in);
public void openfile(){
try {
System.out.print("Enter file location");
String y = Vari.nextLine();
Reader = new Scanner(new File(y));
}catch(Exception e){
System.out.println("Error!!");
}
}
public void readFile(){
while(Reader.hasNextLine()){
String a = Reader.nextLine();
String b = Reader.nextLine();
String c = Reader.nextLine();
String d = Reader.nextLine();
String e = Reader.nextLine();
System.out.printf("This is the LINE PRint\n");
System.out.printf("%s\n%s\n%s\n%s\n", a, b, c, d,e);
break;
}
while(Reader.hasNext()){
String A = Reader.next();
String B = Reader.next();
String C = Reader.next();
String D = Reader.next();
String E = Reader.next();
System.out.printf("\nCharacter Print ");
System.out.printf("%s\n%s\n%s\n%s\n", A, B, C, D,E);
break;
}
}
public void close(){
Reader.close();
}
}
答案 0 :(得分:0)
您的代码中有许多简单的错误,但是阅读它们之后,您的第二个 while
循环没有打印任何内容的基本原因是因为您已经浏览了文本文件并到达了结尾。您需要某种方法来重置文件指针或缓冲区,或者关闭并重新打开一个全新的 Scanner
。
通过初始化两次来纠正这个问题,并纠正小错误,这是工作代码。问题是,这就是你想要达到的目标吗?
stackoverflow.txt:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
StackOverflow.java:
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StackOverflow
{
public static void main( String[] args ) throws FileNotFoundException
{
new StackOverflow("stackoverflow.txt").readNextLines().close();
System.out.println();
new StackOverflow("stackoverflow.txt").readNexts().close();
}
private Scanner reader;
public StackOverflow(String filename) throws FileNotFoundException {
reader = new Scanner( StackOverflow.class.getResourceAsStream( filename ) );
}
public StackOverflow readNextLines()
{
while (reader.hasNextLine())
{
String a = reader.nextLine();
String b = reader.nextLine();
String c = reader.nextLine();
String d = reader.nextLine();
String e = reader.nextLine();
System.out.printf( "This is the LINE print\n" );
System.out.printf( "%s\n%s\n%s\n%s\n%s\n", a, b, c, d, e );
break;
}
return this;
}
public StackOverflow readNexts()
{
while (reader.hasNext())
{
String A = reader.next();
String B = reader.next();
String C = reader.next();
String D = reader.next();
String E = reader.next();
System.out.printf( "Character Print\n" );
System.out.printf( "%s\n%s\n%s\n%s\n%s\n", A, B, C, D, E );
break;
}
return this;
}
public void close()
{
reader.close();
}
}
输出:
This is the LINE print
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Character Print
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ