user3432021
user3432021

Reputation: 3

Can some one help me with this message java Exception?

i have an assignment in java to sort numbers in ascending order and find the max, min , mean and standard deviation

i have done that already but i wanted to change the program to work with double values but there is an exception showing and i cant solve the problem please help can someone fix it.

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);
}



}

and the exception message

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)

and can someone tell me why when i enter 1 it shows 1.0?

Upvotes: 0

Views: 690

Answers (4)

ashokramcse
ashokramcse

Reputation: 2861

Replace this on your for loop of fileoutput() method

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;
    }
 }
}

Upvotes: 1

sheu
sheu

Reputation: 304

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);
}



}

Your FileJava2.output() has been changed solve your issue. Using split to get values then converting string to double instead of int.

Upvotes: 0

DairyLea
DairyLea

Reputation: 837

You are trying to parse each individual character in the string to an integer. After reading the line "1.0 1.0 2.0 5.0" you need to split the numbers and pass the substrings to parse int/double. You can split using the triple whitespace characters like so:

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

Upvotes: 0

donfuxx
donfuxx

Reputation: 11321

This exception:

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

Tells you that you that the String . could not be parsed to an int. And makes sense, because what int could be represented by . ?

Upvotes: 0

Related Questions