Elvis Ve

Greenhorn
+ Follow
since May 17, 2004
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Elvis Ve

hi, how are you all doing?

why does my JFileChooser not write to the file all it does is create it, here is my code, thanks for your help.

SaveAFile(myJFrame f)
{
this.f = f;
String fileName = "";
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(f);

if(result==JFileChooser.CANCEL_OPTION)
JOptionPane.showMessageDialog(f,"you have selected cancel",
"No file saved",JOptionPane.ERROR_MESSAGE);
else if(result == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
Save(getContentsOfFile(file),fileChooser.getSelectedFile());
}

}

void Save(String s , File f)
{

try
{
BufferedWriter out = new BufferedWriter(new FileWriter(f));
out.write(s);
out.close();



}
catch(IOException e)
{
e.toString();
}

}

String getContentsOfFile(File file)
{
String s = new String();
int fileLength = (int)file.length();
char buffer[] = new char[fileLength];
InputStreamReader input;

try
{
int index;
input = new FileReader(file);
while((index=input.read(buffer,0,buffer.length))!=-1)
s = s + new String(buffer,0,index);
}
catch(IOException e)
{
e.toString();
}
return s;
}
13 years ago
I need serious help with this conatenation, I want to concatenate a text file to the tail of another text file, how do i do this, like is there any method that will make this easier or any particular setup, sorry im really confused in this topic, thanks and help in advance.
13 years ago
Im very confused about this topic what I am trying to do is everytime a filepath of a text file is input as a string,the program will write the contents of that file into an output file and everytime a new text file is added it would just concatenate to the end of the output file.
for example:

import java.io.*

class concat
{
File f;

concat(String s)
{
f = new File(s);
}

concatFiles(String a)
{
//this is the method where I want to accept the file path and
write the contents of that text file to an ouput file, so that ouput file will be composed of the text of all the files entered in this method.

try
{
in = new FileInputStream(new FileReade(a));
found = true;
out = new FileOutputStream(C:\\output.txt);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
found = false;
}
//My question is how do i write into the ouput file in such a way that everytime a new file is entered it will concatenate to the tail of the output file?


}
13 years ago
sorry, let me try to be more specific In the program the method grep accepts a String, as soon as you call grep it creates a new BufferedReader of the file, then in the while loop it searches line by line with the search method to find the String in a line, now when it s over it returns the lines that it found the String in but my question is how do also return the line number, here is my search method along with the grep method , hope you can help, thanks in advance:

boolean search(String key, String argument)
{
String s = "";
boolean found = false;
StringTokenizer tok = new StringTokenizer(argument);
while(tok.hasMoreTokens())
{
s = tok.nextToken();
if(key.equalsIgnoreCase(s))
{
found = true;
}
}


return found;


}

void grep(String t)
{
String s = "";
int lineNum = 0;
try
{
b = new BufferedReader(new FileReader(f));
c = new LineNumberReader(new FileReader(f));

while((s=b.readLine())!=null)
{
search(t,s);
if (search(t,s)==true)
line = c.getLineNumber()+ '\t' + line + s + '\n';
}




}
catch(FileNotFoundException e )
{
e.printStackTrace();
}
catch(IOException d)
{
d.printStackTrace();
}
finally
{
try
{
b.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}


}
13 years ago
hey guys how are u all doing? Im having problems using the LineNumberReader class in this follwing method that you will see im trying to print out the line number in front of the actual line of text and all i get is this:
99nulli beg for this thing to work
work is good


this is the method:

void grep(String t)
{
String s = "";
try
{
b = new BufferedReader(new FileReader(f));
c = new LineNumberReader(new FileReader(f));

while((s=b.readLine())!=null)
{
search(t,s);
if (search(t,s)==true)
line = c.getLineNumber()+ '\t' + line + s + '\n';
}




}
catch(FileNotFoundException e )
{
e.printStackTrace();
}
catch(IOException d)
{
d.printStackTrace();
}
finally
{
try
{
b.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}


}

please help, and thanks in advance
13 years ago
thanks alot i took your advice and everything worked fine.
13 years ago
Ok i have two classes one a test class and another one a class called triangle here is the triangle class, when the input dialog comes out i enter the following information: triangle 3 4 5 from that im suppose to be getting the following output "triangle 3 4 5 (the perimeter) (the area), but intead im getting these weird results(39.0--0.012.0-)someone please help here is my triangle class:

class Triangle
{
double s1,s2,s3;

Triangle(double s1, double s2, double s3)
{
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}

double perimeter()
{
return s1 + s2 + s3;
}

double area()
{
double a = 1/2 *(s1 + s2 + s3);
return a*(a-s1)*(a-s2)*(a-s3);
}
}

and here is the test class:

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.util.StringTokenizer;

class Driver
{

public static void main(String[] arg)
{

String s = "";
double s1 = 0,s2= 0,s3=0;
String figure = "";
String str = JOptionPane.showInputDialog(s);
while(str != "")
{

StringTokenizer tok = new StringTokenizer(str);

if(tok.hasMoreTokens())
figure = tok.nextToken();
if(tok.hasMoreTokens())
s1 = Double.parseDouble(tok.nextToken());
if(tok.hasMoreTokens())
s2 = Double.parseDouble(tok.nextToken());
if(tok.hasMoreTokens())
s3 = Double.parseDouble(tok.nextToken());



if(figure.equalsIgnoreCase("Triangle"))

{Triangle b = new Triangle(s1,s2,s3);
System.out.println(s1 + '\t' + s2 +'\t' + s3 + '\t'
+ "-" + b.area() + b.perimeter() + "-" );
}
}
}

}
13 years ago
getData is just another class I have to use the JOption Pane for example here is a part of that class:

class getData
{
static String str;

static String getString(String s)
{
str = JOptionPane.showInputDialog(s);
return str;
}
13 years ago
hey, how are u all doing? I am having problems with the StringTokenizer and I am hoping you all can help, I have a test class that accepts the values of geometric figures in the following order (name of figure, side/radius, side 2, side 3) here is my test class and I want to know why its not working I am pretty sure its something wrong in my StringTokenization :

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.util.StringTokenizer;

class Driver
{

public static void main(String[] arg)
{


String str = getData.getString("Enter data for a figure");
while(str != "")
{

StringTokenizer tok = new StringTokenizer(str);

while(tok.hasMoreTokens())

{
String figure = tok.nextToken();
double s1 = Double.parseDouble(tok.nextToken());
double s2 = Double.parseDouble(tok.nextToken());
double s3 = Double.parseDouble(tok.nextToken());

if(figure.equalsIgnoreCase("Triangle"));
Measurments b = new Triangle(s1,s2,s3);
System.out.println(s1 + '\t' + s2 +'\t' + s3 + '\t'
+ "-" + b.area() + b.perimeter() + "-" );
}

}
}

}
13 years ago
anybody?..... this sort thing is really making me mad
13 years ago
by the way here is my test class just in case i made a mistake there and the sorting is not working because of this:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;


class Driver
{
public static void main(String arg[])
{

Filing f = new Filing();



int m;


do
{
m = getData.getInt("Choose from the follwing menu:\n1- add a Student without GPA. \n2- add a Student with GPA. \n3- change a Student's current GPA\n4-Print\n5-Exit");


switch(m)
{
case 1:
String id = getData.getString("enter student's id");
String lName = getData.getString("enter student's last name");
String fName = getData.getString("enter student's first name");
Student s = new Student(id,lName,fName);
Undergraduate u = new Undergraduate(s);
f.addToList(s);

break;
case 2:
String Id = getData.getString("enter student's id");
String LName = getData.getString("enter student's last name");
String FName = getData.getString("enter student's first name");
double gPA = getData.getDouble("enter the student's GPA");
Student t = new Student(Id,LName,FName,gPA);
Undergraduate n = new Undergraduate(t);
f.addToList(t);
break;
case 3:
String tempID = getData.getString("enter student's id");
double NewGPA = getData.getDouble("enter the student's GPA");
if(f.search(tempID) == true)




break;
case 4:
f.Sort();
JTextArea text = new JTextArea(f.toString(),15,40);
JScrollPane scroll = new JScrollPane(text);
JOptionPane.showMessageDialog(null, scroll,"Student Information", JOptionPane.INFORMATION_MESSAGE);
break;
case 5:
break;
default:
break;


}

}
while(m!=5);



}
}
13 years ago
I made some fixes to it but for some reason it still wont sort, check it out: what is wrong with it?, this is driving me crazy, please help and thanks in advance


void Sort()
{
int length = list.size();
for(int i = 0; i < length -1;i++)
{
int index = findPosition(i);
swap(index,i);
}

}

void swap(int startScan, int i)
{

Student s = (Student)list.get(i);
list.set(i, list.get(startScan));
list.set(startScan, s);

}


int findPosition(int i)
{
int startScan = i;
Student s = (Student)list.get(i);
for(int k =0; k < list.size()-1;k++)
{
Student d = (Student)list.get(k);
if(d.getLname().compareTo(s.getLname())< 0)
startScan = k;



}
return startScan;
}
13 years ago
its not sorting at all, i dont know if i have something wrong inside but everything seems to be right
13 years ago
hey whats up guys, Im having a problem I created a sort method in order to organize the lastNames by alphabetical order here is my sort method: why is it not working when i call the sorth method

void Sort()
{
int length = list.size();
for(int i = 0; i < length -1;i++)
{
int index = findPosition(i);
swap(index,i);
}

}

void swap(int startScan, int i)
{

Student s= (Student)list.get(i);
Student p= (Student)list.get(startScan);
Student temp = s;
s = p;
p = temp;



}


int findPosition(int i)
{
int startScan = i;
Student s = (Student)list.get(i);
for(int k =0; k < list.size()-1;k++)
{
Student d = (Student)list.get(k);
if(d.getLname().compareTo(s.getLname())< 0)
k = startScan;



}
return startScan;
}
13 years ago
im having a problem sorting the ArrayList using selection sort, just need help in the swap method.

void Sort()
{
int length = list.size();
for(int i = 0; i < length -1;i++)
{
int index = findPosition(i);
swap(index,i);
}

}

void swap(int startScan, int i)
{



}


int findPosition(int i)
{
int startScan = 1;
Student s = (Student)list.get(i);
for(int k =0; i < list.size()-1;i++)
{
Student d = (Student)list.get(k);
if(d.getLname().compareTo(s.getLname())< 0);

}
return startScan;
}
13 years ago