「java读取数字」java读取数字,以enter为结束符
今天给各位分享java读取数字的知识,其中也会对java读取数字,以enter为结束符进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java中怎么从文件中读取数
- 2、java中如何从键盘读入一个数
- 3、java中怎么将字符串中的数字取出来
- 4、java中如何提取字符串中的数字?
- 5、JAVA怎么从文件中把数字读取出来并且放到一个数组中呢?
java中怎么从文件中读取数
1.package txt;
2.
3.import java.io.BufferedReader;
4.import java.io.File;
5.import java.io.FileInputStream;
6.import java.io.InputStreamReader;
7.
8./**
9. * 读取TXE数据
10. */
11.public class ReadTxtUtils {
12. public static void main(String arg[]) {
13. try {
14. String encoding = "GBK"; // 字符编码(可解决中文乱码问题 )
15. File file = new File("c:/aa.txt");
16. if (file.isFile() file.exists()) {
17. InputStreamReader read = new InputStreamReader(
18. new FileInputStream(file), encoding);
19. BufferedReader bufferedReader = new BufferedReader(read);
20. String lineTXT = null;
21. while ((lineTXT = bufferedReader.readLine()) != null) {
22. System.out.println(lineTXT.toString().trim());
23. }
24. read.close();
25. }else{
26. System.out.println("找不到指定的文件!");
27. }
28. } catch (Exception e) {
29. System.out.println("读取文件内容操作出错");
30. e.printStackTrace();
31. }
32. }
33.}
java读取TXT文件中的数据,每一行就是一个数,返回一个数组,代码?
?
List list=new ArrayList();
BufferedReader br=new BufferReader(new InputStreamReader(new FileInputStream(new File("in.txt"))));
String str=null;
while((str=br.readLine())!=null)
{
list.add(new Integer(str));
}
Integer[] i=new Integer[list.size()];
list.toArray(i);
TXT文本中如据形如:
123
456
789
读入二维数组效果为:
temp[0][]={1,2,3};
temp[1][]={4,5,6};
temp[2][]={7,8,9};
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.*;
public class xx{
public static void main(String[]args){
String s;
int[][]save=new int[3][3];
try{
BufferedReader in =new BufferedReader(new FileReader("C:\\txt.txt"));
int i=0;
while((s=in.readLine())!=null){
save[i][0]=Integer.parseInt(s.substring(0,1));
save[i][1]=Integer.parseInt(s.substring(1,2));
save[i][2]=Integer.parseInt(s.substring(2,3));
i++;
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
for(int i=0;i3;i++)
{
for(int j=0;j3;j++){
System.out.print(save[i][j]);
}
System.out.println();
}
}
}
或
?
BufferedReader bf=new BufferedReader(new FileReader("Your file"));
String lineContent=null;
int i = 0;
int [][] temp = new int [3][];
while((lineContent=bf.readLine())!=null){
String [] str = lineContent.split("\\d");// 将 lineContent 按数字拆分
for(int j = 0; j str.length(); j++){
int [i][j] = Integer.parseInt(str[j]);
}
i++;
}
scp|cs|ff|201101
这是d:\\a.txt的数据,与“|”分割取数据出来,保存在变量a;b;c;d里
import java.io.*;
public class Test{
public static void main(String[] args)throws Exception{
String a, b, c, d;
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader("d:\\a.txt"));
String s = br.readLine();
while(s != null){
sb.append(s);
s = br.readLine();
}
s = sb.toString();
String[] str = s.split("|");
a = str[0];
b = str[0];
c = str[0];
d = str[0];
}
}
java中如何从键盘读入一个数
1.Scanner
s
=
new
Scanner(System.in);
int
i
=
s.nextInt();
2.
BufferedRader
b
=
new
BufferedReader(new
InputStreamReader(System.in));
int
i
=
Integer.parseInt(b.readLine());
一般我用这两个。
--------------------
s.nextInt()表示只接受int的数据。
还有nextDouble()接收double的数据
因为你输入的内容可能有很多种数据类型,s.nextInt()就自动把你输入的数据转化为int型
java中怎么将字符串中的数字取出来
public class 取数字
{
public static void main(String[] args)
{
System.out.println("\n\t\t==========将字符串中的数字取出来==========\n");
init();
}//初始化!
private static void init()
{
//分割!
String[] s="今天是2017年09月01日".split("\\D+");
//打印分割的!
for (int i=0;is.length ;i++ )
{
System.out.print(s[i]+" ");
}
System.out.println();
}
}
java中如何提取字符串中的数字?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class NumberSplit {
public StringBuffer getString(String str) {
StringBuffer strbuf = new StringBuffer("t");
boolean flag = true;
for (int i = 0; i str.length(); i++) {
char c = str.charAt(i);
// 判断是否是数字
if (c = '0' c = '9') {
// 判断和字符串中的数字是否重复
for (int j = 1; j strbuf.length(); j++) {
if (c == strbuf.charAt(j)) {
// 如果重复,标志位数值为false,并跳出循环;否则标志位设置为true.
flag = false;
break;
} else
flag = true;
}
// 只有在即是数字又不重复的情况下才将改字符拼接到字符串上.
if (flag) {
strbuf.append(c);
}
}
}
return strbuf;
}
public static void main(String args[]) throws Exception {
System.out.println("请输入一段字符串,并以回车结束");
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str = buf.readLine();
StringBuffer strbuf = new NumberSplit().getString(str);
// 将返回的StringBuffer转换为字符数组
char c[] = strbuf.deleteCharAt(0).toString().toCharArray();
// 对字符数组排序
Arrays.sort(c);
// 输出字符数组
for (int i = 0; i strbuf.length(); i++) {
System.out.print(c[i]);
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class NumberSplit {
public StringBuffer getString(String str) {
StringBuffer strbuf = new StringBuffer("t");
boolean flag = true;
for (int i = 0; i str.length(); i++) {
char c = str.charAt(i);
// 判断是否是数字
if (c = '0' c = '9') {
// 判断和字符串中的数字是否重复
for (int j = 1; j strbuf.length(); j++) {
if (c == strbuf.charAt(j)) {
// 如果重复,标志位数值为false,并跳出循环;否则标志位设置为true.
flag = false;
break;
} else
flag = true;
}
// 只有在即是数字又不重复的情况下才将改字符拼接到字符串上.
if (flag) {
strbuf.append(c);
}
}
}
return strbuf;
}
public static void main(String args[]) throws Exception {
System.out.println("请输入一段字符串,并以回车结束");
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str = buf.readLine();
StringBuffer strbuf = new NumberSplit().getString(str);
// 将返回的StringBuffer转换为字符数组
char c[] = strbuf.deleteCharAt(0).toString().toCharArray();
// 对字符数组排序
Arrays.sort(c);
// 输出字符数组
for (int i = 0; i strbuf.length(); i++) {
System.out.print(c[i]);
}
}
JAVA怎么从文件中把数字读取出来并且放到一个数组中呢?
我按照你的代码:
编写1.txt文件:
代码修改为:
结果就是这样的,不是错,因为你的list[]定义的是char数组,可以正确的显示出字符,但是你的num[]定义的是int数组,相当于把char转换成了int,而char转换成int时,实际值为该字符对应的ASCII码。
首先我们查一下ASCII码,找到字符'0'对应的十进制数(即int的值):
是48,也就是说,字符'0'相当于十进制数48,所以读我的1.txtx文件,第一个字符是'1',你的num[j]=list[j]-'0'计算的时候,就是'1'-'0'相当于49-48=1,所以字符1打印出来就是1。
然后我们看我1.txt的第4个字符,是'a',找到'a'的码值:
所以'a'-'0'相当于97-48=49,所以打出来的结果才是49,同理就可以得出了字符'b'、'c'、'!'、'@'、'#'减'0'后的结果了。
java读取数字的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java读取数字,以enter为结束符、java读取数字的信息别忘了在本站进行查找喔。
发布于:2022-11-28,除非注明,否则均为
原创文章,转载请注明出处。