怎么将用户输入的字符串中的空格替换成"*"?并存到当前包下面的文件a.txt中

MIRAI_1900 2019-01-05 01:56:34
输入一串字符(含空格、数字和英文字母),将其中的空格替换成’*’字符后,存入到当前包下面的文件a.txt中。 package qimo;

import java.util.Scanner;

/*
* 输入一串字符(含空格、数字和英文字母),
* 将其中的空格替换成’*’字符后,
* 存入到当前包下面的文件a.txt中。
* */
public class Demo11 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String n;
String n2;
Scanner sc = new Scanner(System.in);
System.out.println("请输入一串字符:");
n = sc.nextLine();
System.out.println(n2);

}

}
...全文
155 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2019-01-06
  • 打赏
  • 举报
回复

String replaceString = s.replace(" ", "*");
林栖者 2019-01-06
  • 打赏
  • 举报
回复
public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); //将空格全部去掉 String replaceString = s.replace(" ", ""); System.out.println(replaceString); //创建字符输出流,路径设置成你自己需要的路径,不设置就是项目的根路径 FileWriter fileWriter = new FileWriter("a.txt"); fileWriter.write(replaceString); fileWriter.close(); }

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧