Solved

Java character count from dialog box text file

Posted on 2009-05-05
16
732 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
  • 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 61
Free Alternative to JIRA 4 58
Strange loading of website behaviour 3 23
maven project error 5 24
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now