import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("src_4/input.txt"));
} catch (IOException e) {
System.out.println("创建输出文件失败");
return;
}
System.out.println("请输入要保存的数据:");
Scanner input = new Scanner(System.in);
String nxt = input.next();
while (!nxt.equals("quit")) {
try {
bw.write(nxt);
bw.write('\n');
} catch (IOException e) {
e.printStackTrace();
}
nxt=input.next();
}
try {
input.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
while(input.next()!="quit"){
str[i++]=input.next(); //提示此处(29行)出错
new Scanner(System.in);
}
改成:
String line = "";
while((line = input.next()) != "quit"){
str[i++]=line;
.....
}
或者
while(input.hasnext()){
String line = input.next();
if("quit".equalsIgnoreCase(line)){
break;
}
str[i++]=line;
.......
}
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
FileWriter fw = null;
List
try {
fw = new FileWriter("e:/aaa.txt");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("请输入要保存的数据:");
Scanner input = new Scanner(System.in);
String tmp="";
while(!(tmp=input.next()).toLowerCase().equals("quit")){
list.add(tmp); //提示此处(29行)出错 没有实际作用只为了替换你的数组
input =new Scanner(System.in);
try {
fw.write(tmp);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(fw!=null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
FileWriter fw = null;
BufferedWriter bw = null;
ListinputList = new ArrayList ();
try {
fw = new FileWriter("src_4/input.txt");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("请输入要保存的数据:");
Scanner input = new Scanner(System.in);
String inputLine = "";
while (!(inputLine = input.next()).equals("quit")) {
inputList.add(inputLine); // 提示此处(29行)出错
new Scanner(System.in);
}
bw = new BufferedWriter(fw);
try {
for (String str : inputList) {
bw.write(str + "\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
str[i++]=input.next(); ,改写成 str[i] = input.next(); i++;
你写的这句i是不自增长的,你可以输出i看一下,改后应该能行,我没测试了,你试下嘛
虽然我不是非常清楚,但是你的String数组应该在一开始就初始化其length,才能够While循环里面的内容!尝试在初始化的时候直接定义合适的长度!