String类型字符串是不是有长度限制,为什么我赋给一个很长的字符串时,输出即只有一半

cds1 2007-07-10 09:24:26
假设我s=kkkkkkkkkkkssssss
输出是s=kkkkkkkkkkkss

以上只是假设,实际上我赋给s的字符串要长得多

求解
...全文
5109 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
在这里,大家都在帮忙了,用不着再开张帖吧!!
  • 打赏
  • 举报
回复
8613719716 这个数字在 long 范围之内的啊,long 的数值长度基本上有 19 位了,一般可以满足要求了,你的数字是什么?
sunggwei 2007-07-10
  • 打赏
  • 举报
回复
BigInteger
cds1 2007-07-10
  • 打赏
  • 举报
回复
试过long型了,还是不行
抛出异常的点还是一样的
  • 打赏
  • 举报
回复
那就使用 long 型的可以吗?
cds1 2007-07-10
  • 打赏
  • 举报
回复
那这个问题要怎么解决呢,我还是要把它转换成自己需要的整型数据,长度也必须那么长
求解
ykrocku 2007-07-10
  • 打赏
  • 举报
回复
>但是把字符减少一位变成"861371971"时又不出抛出异常

基础知识不牢固
java中int是32位,表示不了这么大的数。
你可以使用String 的 parseLong...
  • 打赏
  • 举报
回复
上面的那个代码错在了第二行上,8613719716 这个数字已经超出了 int 类型的范围(-2147483648~2147483647)之间,所以就报异常啰。查看 int 类型的最大值和最小值可以采用 Integer.MAX_VALUE、Integer.MIN_VALUE 来获得,同理 long 类型的也是类似的。

也可以这么说,int 类型的最大值也就是 String 类型的最大长度,因为在 String 的源代码中它的长度就是使用 int 类型的。
weiqiyiji 2007-07-10
  • 打赏
  • 举报
回复
更正一下,int最大值是2147483646,即2的31次方再减一,Math(2,31)-1
weiqiyiji 2007-07-10
  • 打赏
  • 举报
回复
int的最大值是2147483647
你的值超过了这个最大值
cds1 2007-07-10
  • 打赏
  • 举报
回复
太感谢bao的回答了,这个问题已解决

现在有另一个问题请教一下

String line3[1]="8613719716";
int int1=java.lang.Integer.parseInt(line3[1]);

以上是程序中的一小段,但是在运行的过程中总是抛出异常
Exception in thread "main" java.lang.NumberFormatException: For input string: "8613719716"

但是把字符减少一位变成"861371971"时又不出抛出异常,我想问下这到底是不是字符长度的问题,或者是其它的原因
  • 打赏
  • 举报
回复
你的 reader 和 wri 没有关!
cds1 2007-07-10
  • 打赏
  • 举报
回复
但是我的字符长度只有十几k啊,所以绝对超不过限制的


我的程序如下:
import java.io.*;
import java.util.*;
public class modifytxt
{
public static void main(String[] args)
{
File f = new File("tbl_DotServiceCode2.txt");
File f1 = new File("modify.txt");
try
{
InputStreamReader read = new InputStreamReader (new FileInputStream(f));
BufferedReader reader=new BufferedReader(read);
String line0="";
String line;
Properties pp = System.getProperties();
String newLine = pp.getProperty("line.separator");
//String ss=line;
//byte[] bytes=new byte[1024];
for(int i=0;;i++)
{
line=reader.readLine();
if(line==null)
break;
String[] line1=line.split(",");
String[] line2=line1[0].split(" ");
String[] line3=line2[0].split("\"");
int int1=java.lang.Integer.parseInt(line3[1]);
String[] line4=line1[1].split(" ");
String[] line5=line4[0].split("\"");
int int2=java.lang.Integer.parseInt(line5[1].substring(0,line3[1].length()));
if(int1==int2)
{
line0=line0+line+newLine;
//bytes=line.getBytes();
}
else
{
for(int j=0;j<=(int2-int1);j++)
{
System.out.println(j);
String str1=((Integer)(int1+j)).toString();
String str2=str1+line5[1].substring(line3[1].length());
line="\""+str1+" \",\""+str2+" \",\"111 \",\"GSM\"";
line0=line0+line+newLine;
//bytes=ss.getBytes();
}
}
}
OutputStreamWriter wri = new OutputStreamWriter (new FileOutputStream(f1));
//BufferedWriter writer=new BufferedWriter(wri);
wri.write(line0,0,line0.length());
//FileOutputStream fos = new FileOutputStream("modify.txt");
//fos.write(bytes);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
  • 打赏
  • 举报
回复
从 String 类中的 length() 方法返回的是 int 型来看的。
  • 打赏
  • 举报
回复
据说最大的长度是 2GB,不过我没有测试过,内存没有那么大~~
cds1 2007-07-10
  • 打赏
  • 举报
回复
好的,以后要慢慢多用一点
  • 打赏
  • 举报
回复
没有什么了~~,建议你以后使用 Eclipse 工具吧,用 EditPlus 写的话,编写、调试都不是很方便。
cds1 2007-07-10
  • 打赏
  • 举报
回复
又学到了很多东西,bao兄还有什么要说的吗,没有就送分了
  • 打赏
  • 举报
回复
如果源文件中含有空行的话,需要在 While 循环内再加上

if (line.trim().length() < 1) {
  continue;
}
cds1 2007-07-10
  • 打赏
  • 举报
回复
正在理解中…………………………
加载更多回复(13)

62,614

社区成员

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

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