有人可以帮我解决这个消息java Exception吗?

时间:2014-03-18 07:44:08

标签: java

我在java中有一个赋值,按升序对数字进行排序,找出最大值,最小值,平均值和标准差

我已经这样做但我想改变程序以使用双值但是有一个异常显示,我无法解决问题,请帮助可以有人解决它。

import java.io.*;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;


public  class FileJava2 {

static  double min,max,sum,k;
static  double PS1,PS2;
static  double stdev=0;
static  double aa, x[]=new double[1000], no;
static  String source= "",source2= "";

public static void main (String args[]) throws Exception
{
FileJava2.fileinput();
FileJava2.fileoutput();
FileJava2.sort();
FileJava2.display();

}
 public static void sort() throws Exception
{

sum=0;
for(int j=0;j<k;j++){
sum+=x[j];
System.out.print(x[j]+"    ");
}
double t;
 for(int dd=0; dd<k; dd++){

            for(int in=0;in<k-1;in++){
                if(x[dd]<x[in])
                {t=x[dd];
                x[dd]=x[in];
                x[in]=t;
                }
            }
            }
min=x[0];max=0;
 System.out.print("\nSorted Elements: ");
            for(int j=0;j<k;j++){
                    if(x[j]<min)
                        min=x[j];
                    if(x[j]>max)
                        max=x[j];

source2+=x[j];
System.out.print(x[j]+"    ");
source+=x[j];
}
PS1=0;PS2=0;

for( int i=0; i<k;i++) {
    PS1 += x[i];
    PS2 += Math.pow(x[i], 2);
    stdev = Math.sqrt(i*PS2 - Math.pow(PS1, 2))/i;

}
byte buf1[]=source2.getBytes();
OutputStream fo1=new FileOutputStream("SortData.txt");
for (int i=0;i<buf1.length;i++)
{
fo1.write(buf1[i]);
}

}

public static void fileinput() throws Exception{



Scanner s = new Scanner(System.in);
do{
System.out.print("Enter Numbers: ");
aa=s.nextDouble();
if(aa==0)
break;
else
source+=(aa+"   ");
} while(aa!=0);
System.out.println("YOUR INPUT: "+source);
k=0;

byte buf[]=source.getBytes();
OutputStream fo=new FileOutputStream("waitingtime");
for (int i=0;i<buf.length;i++)
{
fo.write(buf[i]);
}
 System.out.println("\nElements successfuly saved into waitingtime.dat  ");

}

public static void fileoutput() throws Exception{
     BufferedReader inputStream = null;
 try {
            inputStream = 
                new BufferedReader(new FileReader("waitingtime"));
            String l;
            while ((l = inputStream.readLine()) != null) {
               // System.out.println(l);
               for ( int i = 0; i < l.length(); i++ ) {   
         String cc=" "+l.charAt( i );  
           x[(int)k++]=Integer.parseInt(cc);  
           //    System.out.println(no);    
            }
        }
 }
        finally {
            if (inputStream != null) {
                inputStream.close();
            }

        } 
}
public static void display(){

System.out.println("\nElements successfuly saved into SortData.dat  ");
System.out.print("\nMinimum: "+min);
System.out.print("\nMaximun:"+max);
System.out.print("\nMean:"+sum/k);
System.out.print("\nMidrange:"+(min+max)/2);
System.out.println("\nStandard Deviation:"+stdev);
}



}

和异常消息

Enter Numbers: 1
Enter Numbers: 1
Enter Numbers: 2
Enter Numbers: 5
Enter Numbers: 0
YOUR INPUT: 1.0   1.0   2.0   5.0   

Elements successfuly saved into waitingtime.dat  
Exception in thread "main" java.lang.NumberFormatException: For input string: "."
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:527)
    at FileJava2.fileoutput(FileJava2.java:110)
    at FileJava2.main(FileJava2.java:21)

有人可以告诉我为什么当我输入1时它会显示1.0?

4 个答案:

答案 0 :(得分:1)

for方法的fileoutput()循环

上替换它
for (int i = 0; i < l.length(); i++) {
 if (l.charAt(i) != '.' && l.charAt(i) != ' ') {
    String cc = (" " + l.charAt(i)).trim();
    int result = Integer.parseInt(cc);
    if (result != 0) {
      x[(int) k++] = result;
    }
 }
}

答案 1 :(得分:0)

此例外:

java.lang.NumberFormatException: For input string: "."
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)

告诉您,无法将字符串.解析为int。 并且有道理,因为int可以由.表示?

答案 2 :(得分:0)

您正在尝试将字符串中的每个字符解析为整数。在读取行"1.0 1.0 2.0 5.0"之后,您需要拆分数字并将子串传递给解析int / double。您可以使用三个空格字符进行拆分,如下所示:

 while ((l = inputStream.readLine()) != null) {
     for(String ss:l.split("   ") {
         x[(int)k++] = Double.parseDouble(ss); 
     }
 }

答案 3 :(得分:0)

import java.io.*;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;


public  class FileJava2 {

static  double min,max,sum,k;
static  double PS1,PS2;
static  double stdev=0;
static  double aa, x[]=new double[1000], no;
static  String source= "",source2= "";

public static void main (String args[]) throws Exception
{
FileJava2.fileinput();
FileJava2.fileoutput();
FileJava2.sort();
FileJava2.display();

}
 public static void sort() throws Exception
{

sum=0;
for(int j=0;j<k;j++){
sum+=x[j];
System.out.print(x[j]+"    ");
}
double t;
 for(int dd=0; dd<k; dd++){

            for(int in=0;in<k-1;in++){
                if(x[dd]<x[in])
                {t=x[dd];
                x[dd]=x[in];
                x[in]=t;
                }
            }
            }
min=x[0];max=0;
 System.out.print("\nSorted Elements: ");
            for(int j=0;j<k;j++){
                    if(x[j]<min)
                        min=x[j];
                    if(x[j]>max)
                        max=x[j];

source2+=x[j];
System.out.print(x[j]+"    ");
source+=x[j];
}
PS1=0;PS2=0;

for( int i=0; i<k;i++) {
    PS1 += x[i];
    PS2 += Math.pow(x[i], 2);
    stdev = Math.sqrt(i*PS2 - Math.pow(PS1, 2))/i;

}
byte buf1[]=source2.getBytes();
OutputStream fo1=new FileOutputStream("SortData.txt");
for (int i=0;i<buf1.length;i++)
{
fo1.write(buf1[i]);
}

}

public static void fileinput() throws Exception{



Scanner s = new Scanner(System.in);
do{
System.out.print("Enter Numbers: ");
aa=s.nextDouble();
if(aa==0)
break;
else
source+=(aa+"   ");
} while(aa!=0);
System.out.println("YOUR INPUT: "+source);
k=0;

byte buf[]=source.getBytes();
OutputStream fo=new FileOutputStream("waitingtime");
for (int i=0;i<buf.length;i++)
{
fo.write(buf[i]);
}
 System.out.println("\nElements successfuly saved into waitingtime.dat  ");

}

public static void fileoutput() throws Exception{
     BufferedReader inputStream = null;
 try {
            inputStream = 
                new BufferedReader(new FileReader("waitingtime"));
            String l;
            while ((l = inputStream.readLine()) != null) {
               // System.out.println(l);
                String numbers[] = l.split("   ");
                for (String cc : numbers) {
                    x[(int)k++]=Double.parseDouble(cc);
                    //    System.out.println(no);    
                }
        }
 }
        finally {
            if (inputStream != null) {
                inputStream.close();
            }

        } 
}
public static void display(){

System.out.println("\nElements successfuly saved into SortData.dat  ");
System.out.print("\nMinimum: "+min);
System.out.print("\nMaximun:"+max);
System.out.print("\nMean:"+sum/k);
System.out.print("\nMidrange:"+(min+max)/2);
System.out.println("\nStandard Deviation:"+stdev);
}



}

您的FileJava2.output()已更改解决您的问题。使用split获取值,然后将string转换为double而不是int。