The Artima Developer Community
Sponsored Link

Java Answers Forum
can anyone help to find the error in the code

1 reply on 1 page. Most recent reply: Mar 10, 2002 6:50 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
jayakumar perumal

Posts: 16
Nickname: jai
Registered: Feb, 2002

can anyone help to find the error in the code Posted: Mar 10, 2002 1:30 AM
Reply to this message Reply
Advertisement
actually iam trying to read a text file and manipulate the data in it but iam not able to get the vector transfered into array it just copys the first line alone
iam attaching the prog acn anyone help i will appreciate


import java.io.*;
import java.math.*;
import java.awt.*;
import java.util.*;

public class tempcon
{
double val;
double sq;
double fin;
String str="";
String temp="";
Integer value;
StringTokenizer stToken;
int temp1,temp2,temp3,temp4,temp5,x1,x2;
int[] array;
int[] test;
String s1;
int[] x;
int largestValue;
int smallestValue;
int largestValueIndex;
int smallestValueIndex;
Vector dummy=new Vector();

public static void main(String arg[])throws IOException
{
tempcon t=new tempcon();
t.readinput();
t.vectorToIntArray();
t.FindMax();
//t.FindMin();
}

public void readinput()
{

try
{

DataInputStream br=new DataInputStream(new BufferedInputStream(new FileInputStream("file.txt")));

while((str=br.readLine())!=null)
{
stToken= new StringTokenizer(str," ",false);
while(stToken.hasMoreTokens())
{
temp = stToken.nextToken();
if(temp!=null)
{
//value = new Integer(temp);
dummy.addElement(temp);
}
}
}


}
catch(Exception e)
{
System.out.println("exception");
}

}

public void vectorToIntArray()
{
Vector v = new Vector();
v = dummy;
{
array = new int[v.size()];
Enumeration e = v.elements();
for( int i=0; e.hasMoreElements(); i++ )
{
try
{
array = ((Integer)e.nextElement()).intValue();

}
catch(ClassCastException cce)
{
array = 0;
}
}
}
for(int i=0;i<array.length;i++)
{
//System.out.println(" a :"+array);
double val=(0.3464-(0.0692*(683.72-array)));
double sq=Math.sqrt(val);
double fin=(-0.1732+sq)/(2*0.01732);
System.out.println(" the value of fin :"+fin);
}
}

// to findthe largest value of the temperature array

public void FindMax()
{
largestValue = array[0];

for(int i = 1; i<array.length; i++)
{
if (largestValue < array) {
largestValue = array;
largestValueIndex = i;
}
}

}


//to find the smallest value of temaperature array
public void FindMin()
{
smallestValue = array[0];
for(int i = 1; i<array.length; i++)
{
if (smallestValue > array)
{
smallestValue = array;
smallestValueIndex =i;
}
}
}


}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: can anyone help to find the error in the code Posted: Mar 10, 2002 6:50 AM
Reply to this message Reply
The lines I revised in you code are commented out with // and replaced with the new line right below it.


import java.io.*;
import java.math.*;
import java.awt.*;
import java.util.*;

public class tempcon
{
double val;
double sq;
double fin;
String str="";
String temp="";
Integer value;
StringTokenizer stToken;
int temp1,temp2,temp3,temp4,temp5,x1,x2;
int[] array;
int[] test;
String s1;
int[] x;
int largestValue;
int smallestValue;
int largestValueIndex;
int smallestValueIndex;
Vector dummy=new Vector();

public static void main(String arg[])throws IOException
{
tempcon t=new tempcon();
t.readinput();
t.vectorToIntArray();
t.FindMax();
//t.FindMin();
}

public void readinput()
{

try
{

//DataInputStream br=new DataInputStream(new BufferedInputStream(new FileInputStream("file.txt")));
BufferedReader br=new BufferedReader(new FileReader("file.txt"));

while((str=br.readLine())!=null)
{
stToken= new StringTokenizer(str," ",false);
while(stToken.hasMoreTokens())
{
temp = stToken.nextToken();
if(temp!=null)
{
//value = new Integer(temp);
dummy.addElement(temp);
}
}
}


}
catch(Exception e)
{
System.out.println("exception");
}

}

public void vectorToIntArray()
{
Vector v = new Vector();
v = dummy;
{
array = new int[v.size()];
Enumeration e = v.elements();
for( int i=0; e.hasMoreElements(); i++ )
{
try
{
//array = ((Integer)e.nextElement()).intValue();
array[i] = ((Integer)e.nextElement()).intValue();

}
catch(ClassCastException cce)
{
//array = 0;
array[i] = 0;
}
}
}
for(int i=0;i<array.length;i++)
{
//System.out.println(" a :"+array);
//double val=(0.3464-(0.0692*(683.72-array)));
double val=(0.3464-(0.0692*(683.72-array[i])));
double sq=Math.sqrt(val);
double fin=(-0.1732+sq)/(2*0.01732);
System.out.println(" the value of fin :"+fin);
}
}


// to findthe largest value of the temperature array

public void FindMax()
{
largestValue = array[0];

for(int i = 1; i<array.length; i++)
{
//if (largestValue < array) {
if (largestValue < array[i]) {
//largestValue = array;
largestValue = array[i];
largestValueIndex = i;
}
}

}


//to find the smallest value of temaperature array
public void FindMin()
{
smallestValue = array[0];
for(int i = 1; i<array.length; i++)
{
//if (smallestValue > array)
if (smallestValue > array[i])
{
//smallestValue = array;
smallestValue = array[i];
smallestValueIndex =i;
}
}
}


}

Flat View: This topic has 1 reply on 1 page
Topic: problem in uploading files Previous Topic   Next Topic Topic: Indention & Comments

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use