i need an expert to double check my program and give me feedback and what works?
ID: 3651186 • Letter: I
Question
i need an expert to double check my program and give me feedback and what works?how to use tokenizer to split word in text file?
i ended up using gridLayout but i would like to use FlowLayout instead is it the same way?
//--> cannot find variable selectedFile. error
FileReader i= new FileReader(selectedFile);
File output = chooser.getSelectedFile();
FileWriter o = new FileWriter(output);
String out = selectedFile.getName(); //getname of file
try
---> cannot find symbol method new bufferedReader (java.ioFile)
(BufferedReader reader = File.newBufferedReader(selectedFile)){
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException output) {
System.err.format("IOException: %s%n", output);
}
----------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.Scanner.*;
public class FileChooserGUI extends JFrame
{
JButton button1;
JPanel panel;
JLabel label1, label2, label3;
//input = inFile.readLine(); //read a line
FileChooserGUI()
{ //Constructor
panel = new JPanel(new GridLayout(9,10)); //first one increase height
//second one increase width
panel.setSize(300,200);
label1 = new JLabel("the number of Words: "); //1st label
label2 = new JLabel("the number of Lines: "); //2nd label
label3 = new JLabel("Target search Word: "); //3rd label
button1 = new JButton("START");
panel.add(label1); //adding three labels to the window frame
panel.add(label2); //adding button 2
panel.add(label3); //adding button 3
panel.add(button1); //adding 1 button to window frame
add(panel); //adding panel to window
label1.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
setVisible(true);
pack();
//button1.addActionListener(new ActionListener());
}
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//chooser.setCurrentDirectory(new File(location));
//button1.addActionListener(new ActionListener());
}
public class transferFiles
{
public static void main(String args[])throws Exception{
JFileChooser chooser = new JFileChooser();
int wordCount = 0;
int code = chooser.showOpenDialog(null);
if (code == JFileChooser.APPROVE_OPTION)
{
File selectedFile = chooser.getSelectedFile();
Scanner input = new Scanner(selectedFile);
//--> cannot find variable selectedFile. error
FileReader i= new FileReader(selectedFile);
File output = chooser.getSelectedFile();
FileWriter o = new FileWriter(output);
String out = selectedFile.getName(); //getname of file
try
(BufferedReader reader = File.newBufferedReader(selectedFile)){
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException output) {
System.err.format("IOException: %s%n", output);
}
//}
//while(i.hasNext())
//{
// o.write(i);
//}
//while (input.hasNext())
//{ //while loop to count all thw words in txt file
// o.Write(i);
// ++wordCount;
// input.next();
//}}
//}catch (Exception output)
//{ //catch an error on getting name of file
// output.printStackTrace();
//}
label1.setText("File Name is : " + output);
label2.setText("File Size is : " +Long.toString(size));
label3.setText("word target : " +Integer.toString(wordCount));
label1.setVisible(true);
label2.setVisible(true);
label3.setVisible(true);
}
}}}
Explanation / Answer
import java.util.StringTokenizer; public class Token { private String string = "An=Example=String;" + "String=Tokenizer;" public static void main(String args[]) { StringTokenizer token = new StringTokenizer(string, "=;"); while(token.hasMoreTokens()) { String st1 = string.nextToken(); String st2 = string.nextToken(); System.out.println(st1 + " " + st2); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.