Solved

Java character count from dialog box text file

Posted on 2009-05-05
16
753 Views
Last Modified: 2013-11-23
Hello all,

i am still a newbie to this, my java programming skills are younger than a week so please excuse me if i sound stupid.

I need to write a java program that when it runs it prompts the user to brows for a file with the JFileChooser function once the user selects a text file it should display on the screen

 - total number of characters, including spaces in the line
 - number of times the number 4 appeared in the line
 - reversed string
 - everything should be separated by a comma, in the same line

so for example if the line contains expert4exchange it should show 15,1,egnahcxe4trepxe

the 15 represent the character count, the 1 represent only one time the number 4 was read, and the rest is the word in reverse

the output should be saved on another file and should have the same amount of lines the first file does but only the modifications of each line

the code i did below is not complete, any hints would be greatly appreciated.
import java.util.Scanner;
import javax.swing.JFileChooser;
 
public class ReadFileUsingJFileChooser {
	public static void main(String[] args) throws Exception {
		
		JFileChooser fileChooser = new JFileChooser();
		if (fileChooser.showOpenDialog(null)
			== JFileChooser.APPROVE_OPTION ) {
			
			java.io.File file = fileChooser.getSelectedFile();
			
			Scanner input = new Scanner(file);
			
			while (input.hasNext()) {
				System.out.println(input.nextLine());
				}
				
			input.close();
			}
			else {
				System.out.println("no file selected");
				
				}
			}
		}

Open in new window

0
Comment
Question by:akhalidy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
  • +1
16 Comments
 

Author Comment

by:akhalidy
ID: 24309875
I did the first part where the code basically runs and asks the user to browse for a text file, once that text file is opened it displays every line on the screen, the code is attached in my first post. after this im really confused, any help would be appreciated.
0
 
LVL 92

Expert Comment

by:objects
ID: 24310187
- total number of characters, including spaces in the line

int nchars = line.length();

 - number of times the number 4 appeared in the line

loop for each char in string and increemnt counter whenever you find a 4

 - reversed string

http://helpdesk.objects.com.au/java/how-to-reverse-a-string

0
 

Author Comment

by:akhalidy
ID: 24316295
can somebody please shed more light, im still new to java and exactly sure how to plug the code in
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 4

Accepted Solution

by:
my2eggs earned 500 total points
ID: 24316532
I would break up some of your tasks into methods.

- Total number of characters in the string (as objects mentioned in an earlier post) :

int nchars = line.length();

- Number of times a character shows up in the string and reverse the string, use the methods below:

Then you just need to print out the results or write it to a file.

	public String reverseString(String s) {
		StringBuilder sb = new StringBuilder(s);
		sb.reverse();
		return sb.toString();
	}
	
	public int countOccurences(char match, String s) {
		int count = 0;
		
		for(int i = 0; i < s.length(); i++) {
			if(s.charAt(i) == match) {
				count++;
			}
		}
        }

Open in new window

0
 
LVL 4

Expert Comment

by:my2eggs
ID: 24316560
For a quick code sample of how to write to a file use the following:

http://www.javadb.com/write-to-file-using-bufferedwriter
0
 
LVL 4

Expert Comment

by:my2eggs
ID: 24318856
Note: I forgot to return the result for the countOccurences method. It should look like this :
        public int countOccurences(char match, String s) {
                int count = 0;
                
                for(int i = 0; i < s.length(); i++) {
                        if(s.charAt(i) == match) {
                                count++;
                        }
                }
 
                return count;
        }

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24320907
> can somebody please shed more light

I'm not allowed to write it for, its against this sites rules

> im still new to java and exactly sure how to plug the code in

give it a go and if you get stuck I'll point you in right direction

0
 

Expert Comment

by:yarabati
ID: 24327140
Re: the output should be saved on another file and should have the same amount of lines the first file does but only the modifications of each line

Can you help me to understand this?

If file has below lines:
Welcome to Java
experts4exchange

What are you expecting in another file?

0
 

Author Comment

by:akhalidy
ID: 24331992
thank you guys, sorry i wasnt near a PC for the past 2 days, i will give your suggestions a shot now and post back, i also wasnt trying to have somebody write it for me, just a point to the right direction.
0
 

Author Comment

by:akhalidy
ID: 24332012
yarabati, i meant the output should be saved in a txt file
0
 

Author Comment

by:akhalidy
ID: 24332035
ok, so this is what i tried (my2eggs way) but it still doesnt compile or run, i believe im almost there just need somebody to give me a hand, thanks in advanced.
import java.util.Scanner;
import javax.swing.JFileChooser;
 
public class ReadFileUsingJFileChooser {
	public static void main(String[] args) throws Exception {
		
		JFileChooser fileChooser = new JFileChooser();
		if (fileChooser.showOpenDialog(null)
			== JFileChooser.APPROVE_OPTION ) {
			
			java.io.File file = fileChooser.getSelectedFile();
			
			Scanner input = new Scanner(file);
			int nchars = line.length();
			
			while (input.hasNext()) {
				System.out.println(input.nextLine());
				}
				   public String reverseString(String s) {
                StringBuilder sb = new StringBuilder(s);
                sb.reverse();
                return sb.toString();
        }
        
        public int countOccurences(char match, String s) {
                int count = 0;
                
                for(int i = 0; i < s.length(); i++) {
                        if(s.charAt(i) == match) {
                                count++;
                        }
                }
					                 return count;
 
					         }
 
				
			input.close();
			}
			else {
				System.out.println("no file selected");
				
				
				}
			}
		}

Open in new window

0
 
LVL 4

Expert Comment

by:my2eggs
ID: 24335306
You cannot nest methods inside of methods. The methods I gave you should go outside of the "main" method. Here is a quick tutorial on how methods in Java works :

http://video-animation.com/java_011.shtml
(Don't pay attention to the flash video tutorial, just read through the page.)

0
 
LVL 4

Expert Comment

by:my2eggs
ID: 24335516
Another thing you might want to read up on is Java coding styles. As a new Java programmer it is good to get into the habit of creating code that is easy to read. These are just guidelines and are sometimes a matter of preference but you will find that it is much easier to debug your own code if you are following a coding convention that allows you to read your code without constantly looking up variables names and counting brackets.

For a site on coding conventions that is easy to read and in plain english go here:
http://www.cwu.edu/~gellenbe/javastyle/index.html

This is the official Sun java coding conventions:
http://java.sun.com/docs/codeconv/
0
 

Author Comment

by:akhalidy
ID: 24336723
I did what you said and now the code compiles and run just fine, but it doesnt convert, reverse or do anything other than read the info in the text file like how i had it in my original post
import java.util.Scanner;
import javax.swing.JFileChooser;
 
public class ReadFileUsingJFileChooser {
	public static void main(String[] args) throws Exception {
		
		JFileChooser fileChooser = new JFileChooser();
		if (fileChooser.showOpenDialog(null)
			== JFileChooser.APPROVE_OPTION ) {
			
			java.io.File file = fileChooser.getSelectedFile();
			
			Scanner input = new Scanner(file);
 
			
			while (input.hasNext()) {
				System.out.println(input.nextLine());
				}
				
			input.close();
			}
			else {
				System.out.println("no file selected");
				
				
				}
			}
			
			public String reverseString(String s) {
            StringBuilder sb = new StringBuilder(s);
             sb.reverse();
                return sb.toString();
        }
        
        public int countOccurences(char match, String s) {
          int count = 0;
                
          for(int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == match) {
               count++;
                  }
              }
				return count;
 
		 }
  }

Open in new window

0
 
LVL 4

Expert Comment

by:my2eggs
ID: 24336996
It's because you aren't actually using the methods I provided. You now have them in your code correctly but they won't do anything if you don't ever call those methods.

I would go to the link I suggested before and read through it thoroughly. In particular read the section on Java Method Return Types. Make sure you understand everything on that page before continuing on. If something on that page is confusing feel free to ask about here and I will help you understand it.
0
 
LVL 4

Expert Comment

by:my2eggs
ID: 24337012
Just to clarify, I am talking about this link:

http://video-animation.com/java_011.shtml
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question