10 Replies - 505 Views - Last Post: 06 October 2012 - 04:26 AM Rate Topic: -----

#1 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Java

Posted 29 September 2012 - 06:55 AM

okay i was able to fix that.
basically my GUI should ask user to input a file name then output a file name,
then look for a search word inside the (notepad) text file.

GUI
public class GUI
{
  
//string variables to store file names
     String readFile,writeFile;
     //method to encrypt the given file by adding 5
     public void GUI(Scanner scan)
     {
         //reading input and output filenames
         System.out.println("Enter Input file name");
         readFile=scan.next();
         System.out.println("Enter Output file name");
         writeFile=scan.next();
         
         try
         {
             //opening files
         FileInputStream  in=new FileInputStream(readFile);
         FileOutputStream out=new FileOutputStream(writeFile);
        
         //closing files
         in.close();
         out.close();
        }
         catch(Exception e)
         {
             //if any exception generates displays on console
             System.out.println(e.toString());
         }
}

public static void main(String[] args){
             //user has to enter input file name;
             Scanner scan = new Scanner(System.in);
             System.out.println("Enter input file name");
             System.out.println("Enter output file name");
 }
    }



after i need help with my other class that i attached
Read a input file and look for how many times a word happen
Copy the input file to the output file and putting word from input to output txt
Output # of words in input, the # of lines in input and the # of times the search word comes.

a word is a string of characters end with blank space.

This post has been edited by jon.kiparsky: 05 October 2012 - 10:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Java

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Java

Posted 29 September 2012 - 06:58 AM

Please write better thread titles. The title "Java" is immeasurably pointless.

What help do you need?
Was This Post Helpful? 2
  • +
  • -

#3 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: Java

Posted 29 September 2012 - 07:24 AM

i need help to make my GUI ask user to input a file name then output a file name?
im trying to think if i should use Joptionpane.
i need for it to look for a search word inside a notepad txt file?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

Re: Java

Posted 29 September 2012 - 07:31 AM

Quote

im trying to think if i should use Joptionpane.

You could do. Then use a JFileChooser in a menu item like normal programs

This post has been edited by g00se: 29 September 2012 - 07:31 AM

Was This Post Helpful? 2
  • +
  • -

#5 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: Java

Posted 30 September 2012 - 08:54 AM

Basically i want an expert to take a look at it.
and i just need to know how to do a JOptionPane to do an input and output txt file in my GUI?

I added a counter to look for text in my filecopy class, does it make sense?
i have a 2nd class that look (read and write from txt1 to txt2) how to implement it to my GUI?
how to use get and set method for that?

  GUI
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FileSamplePanel {

  public static void main(String args[]) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JLabel directoryLabel = new JLabel(" ");
    directoryLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
    contentPane.add(directoryLabel, BorderLayout.NORTH);

    final JLabel filenameLabel = new JLabel(" ");
    filenameLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
    contentPane.add(filenameLabel, BorderLayout.SOUTH);

    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(false);
    contentPane.add(fileChooser, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        JFileChooser theFileChooser = (JFileChooser) actionEvent
            .getSource();
        String command = actionEvent.getActionCommand();
        if (command.equals(JFileChooser.APPROVE_SELECTION)) {
          File selectedFile = theFileChooser.getSelectedFile();
          directoryLabel.setText(selectedFile.getParent());
          filenameLabel.setText(selectedFile.getName());
        } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
          directoryLabel.setText(" ");
          filenameLabel.setText(" ");
        }
      }
    };
    fileChooser.addActionListener(actionListener);

    frame.pack();
    frame.setVisible(true);
  }
}




2nd class
import java.io.BufferedWriter;                  //importing library tools
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.io.*;

//copies words from one notepad text to another notepad text
public class CopyFile {
private int wordCount;
private int characterCount;

public CopyFile()
{
}

public void read(Scanner in){
while(in.hasNextLine())
{
         String next = in.next();
         characterCount += next.length();
         wordCount++;
}
}

public int getWordCount(){
   return wordCount;
}

public static void main(String[] args) {       //need for program to run
       File in = new File("input.txt");                  //textfile 1
       File out = new File("output.txt");                 //textfile 2
 try     //code that handle errors and exceptions from method code
 {
     Scanner input = new Scanner(in);
     BufferedWriter writer = new BufferedWriter(new FileWriter(out));
     //Keeps track of line number
     int line = 1;
     //While there are still lines to read
     while(input.hasNextLine())
     {
       writer.write(line + "\t" + input.nextLine()); //from txt
       writer.newLine();
       line++; //Increment line            //total lines added
     }
     input.close();
     writer.close();
     System.out.println("file copied successfully");
 }
 catch(FileNotFoundException e1) {//In file does not exist
 e1.printStackTrace();
 //helps trace exception, identify which method cause bug
        }
 catch (IOException e2) { //Something strange happens with io
 e2.printStackTrace();
 }



Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

Re: Java

Posted 30 September 2012 - 09:35 AM

Quote

and i just need to know how to do a JOptionPane to do an input and output txt file in my GUI?


The purpose of the JOptionPane would merely be to give the user an instruction to choose a file. That's all

This post has been edited by g00se: 30 September 2012 - 09:35 AM

Was This Post Helpful? 1
  • +
  • -

#7 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: Java

Posted 05 October 2012 - 07:51 AM

okay the error that occurs is
public class transferFiles
{
public static void main(String args[])throws Exception{
try{
---> int code = chooser.showOpenDialog(null);
if (code == JFileChooser.APPROVE_OPTION)
{
File selectedFile = chooser.getSelectedFile();
Scanner input = new Scanner(selectedFile);
}

cannot find symbol - variable chooser.
i should have post the error all along.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

Re: Java

Posted 05 October 2012 - 08:01 AM

You need to create the chooser in main
Was This Post Helpful? 1
  • +
  • -

#9 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: Java

Posted 05 October 2012 - 08:59 AM

it is fix.
Another error came up
----> while (input.hasNext())
{ //while loop to count all thw words in txt file

++wordCount;
input.next();
}

cannot find symbol: variable input.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

Re: Java

Posted 05 October 2012 - 09:39 AM

You don't post much code. I'd guess the same fix as before should work
Was This Post Helpful? 0
  • +
  • -

#11 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: Java

Posted 06 October 2012 - 04:26 AM

cannot find symbol method hasNext()


      try{
     int code = chooser.showOpenDialog(null);
      if (code == JFileChooser.APPROVE_OPTION)
     {
      File selectedFile = chooser.getSelectedFile();
      Scanner input = new Scanner(selectedFile);
     
FileReader i= new FileReader(selectedFile);
BufferedWriter ra = new BufferedWriter(new FileWriter("C:\\Users\\User\\Desktop"));

cannot find symbol method hasnext() error
----->>>>>    while(i.hasNext())
{
  o.write(i);
} 

String out = selectedFile.getName(); //getname of file


This post has been edited by ric1989: 06 October 2012 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1