473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

help on setting up letter counter with joptionpane dialog boxes

5 New Member
What I'm bad at is the dialog box part. This is the code Ive come up with... if you see a way to improve what im thinkng of.... post it please.

Program should....
1. Ask user to type in a sentence, using JOptionPane.showInputDialog().
2. The program will scan each letter in the string and count how many times the upper case letter 'E' appears, and how many times the lower case letter 'e' appears.
3. Using a JOptionPane.showInputDialog(), tell the user how many upper and lower case e's were in the string.
4. Repeat this process until the user types the word 'Stop'

import javax.swing.*;
public class Project0 {
public static void main (String args[]) {
String line = JOptionPane.showInputDialog("Enter a line of text:");
// Checks if input has minimum of 1 character //if (line.length() == 0) // System.exit(0);
if (line.length() !=0 || line.length() ==0 ){ // If input is 'Stop' (case sensitivite) // program will terminate.
if(line.length() == 4 && line.charAt(0) == 83 && line.charAt(1) == 116 && line.charAt(2) == 111 && line.charAt(3) == 112 ) System.exit(0);
} // End check for "Stop" input
JOptionPane.showMessageDialog(null, "Your string contains " + uppercaseCount(line) + " uppercase letters 'E' and " + lowercaseCount(line) + " lowercase letters 'e'.");
} //end main // This method goes through the input "line" character by character // and keeps count of which are _UPPERCASE_ letters
}
}
}
// using charAt to cycle through each letter public static int uppercaseCount(String line)
{ int upperCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 69 ) { upperCount++;
} //end if()
} //end loop
return upperCount; } //end uppercaseCount
public static int lowercaseCount(String line) {
int lowerCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 101 ) { lowerCount++; } //end if()
} //end loop
return lowerCount; } //end uppercaseCount
} //end class
}


ehh it didnt come out looking clean here... but put it on eclipse or jdk... i think it works? oh and got to use that number system instead of just char 'e' string, etc.

thanks whomever understands this.
Sep 15 '07 #1
3 5038
r035198x
13,262 MVP
What I'm bad at is the dialog box part. This is the code Ive come up with... if you see a way to improve what im thinkng of.... post it please.

Program should....
1. Ask user to type in a sentence, using JOptionPane.showInputDialog().
2. The program will scan each letter in the string and count how many times the upper case letter 'E' appears, and how many times the lower case letter 'e' appears.
3. Using a JOptionPane.showInputDialog(), tell the user how many upper and lower case e's were in the string.
4. Repeat this process until the user types the word 'Stop'

import javax.swing.*;
public class Project0 {
public static void main (String args[]) {
String line = JOptionPane.showInputDialog("Enter a line of text:");
// Checks if input has minimum of 1 character //if (line.length() == 0) // System.exit(0);
if (line.length() !=0 || line.length() ==0 ){ // If input is 'Stop' (case sensitivite) // program will terminate.
if(line.length() == 4 && line.charAt(0) == 83 && line.charAt(1) == 116 && line.charAt(2) == 111 && line.charAt(3) == 112 ) System.exit(0);
} // End check for "Stop" input
JOptionPane.showMessageDialog(null, "Your string contains " + uppercaseCount(line) + " uppercase letters 'E' and " + lowercaseCount(line) + " lowercase letters 'e'.");
} //end main // This method goes through the input "line" character by character // and keeps count of which are _UPPERCASE_ letters
}
}
}
// using charAt to cycle through each letter public static int uppercaseCount(String line)
{ int upperCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 69 ) { upperCount++;
} //end if()
} //end loop
return upperCount; } //end uppercaseCount
public static int lowercaseCount(String line) {
int lowerCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 101 ) { lowerCount++; } //end if()
} //end loop
return lowerCount; } //end uppercaseCount
} //end class
}


ehh it didnt come out looking clean here... but put it on eclipse or jdk... i think it works? oh and got to use that number system instead of just char 'e' string, etc.

thanks whomever understands this.
Have you tried it?
Try it on the computer, pay special attention to any error messages you get. Post them here if you don't understand them or fail to remove them.
Sep 15 '07 #2
majorecono
5 New Member
ahahah it doesnt run for some odd reason, im guessing i fuked up some of my curlies... but im trying to find out if i made some obvious errors... or made it harder for myself by using a long code, thanks though
Sep 16 '07 #3
majorecono
5 New Member
import javax.swing.*;
public class Project0 {
public static void main (String args[]) {
String line = JOptionPane.showInputDialog("Please enter a sentence.");
// Checks if input has minimum of 1 character //if (line.length() == 0) // System.exit(0);
if (line.length() !=0 || line.length() ==0 ){ // If input is 'Stop' (case sensitivite) // program will terminate.
if(line.length() == 4 && line.charAt(0) == 83 && line.charAt(1) == 116 && line.charAt(2) == 111 && line.charAt(3) == 112 )
System.exit(0); } // End check for "Stop" input
JOptionPane.showMessageDialog(null, "Number of lower case e's: "+uppercaseCount(line)+" Number of upper case e's: "+lowercaseCount(line)+" lowercase letters 'e'."); } //end main
// This method goes through the input "line" character by character
// and keeps count of which are _UPPERCASE_ letters




{
// using charAt to cycle through each letter public static int uppercaseCount(String line)
{ int upperCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 69 ) { upperCount++;
} //end if()
} //end loop
return upperCount; } //end uppercaseCount
public static int lowercaseCount(String line) {
int lowerCount = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x == 101 ) { lowerCount++; //end if()
} //end loop
return lowerCount; } //end uppercaseCount
} //end class
}

My new code is looking like this ... but Im having 2 errors.... one being (The method uppercaseCount(string) is undefined for the type Project0) I dont know what that means on Eclipse.

And the other problem im having is having my output show on 2 lines..
"Number of lower case e's: "+uppercaseCount(line)+" Number of upper case e's: "+lowercaseCount(line)+" lowercase letters 'e'."

Please do help..
Sep 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: SCOTT DELIBAC | last post by:
Can Anyone Help?? I have this program which is listed below. I want to take the grades that the students have assigned to them, and assign the letter grades a numeric value so that I can calculate...
1
aprilmae36
by: aprilmae36 | last post by:
Nice day to all... I'm new in java programming and I'm having some problem with this program. The problem is this: Write a program that will read an unspecified number of non-zero integers and...
1
by: Unebrion | last post by:
Alright im working on a program that prints out user imput in a frame, along with a barcode.. it is like the front of an envelope. Here is the description for the program. This...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
1
by: R69D | last post by:
Write and test the following method that implements the power function: static double pow(double x, int n) This method returns the value of x raised to the power n. For example pow(2.0, -3)...
7
helpwithcode
by: helpwithcode | last post by:
Hi people, I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements, String string_dob=text_dob.getText(); //Converting string to...
1
by: saytri | last post by:
I have a problem with this code. I'm, doing a quiz, and the questions are stored in a binary file. My problem is how am i going to compare the answers entered by the user to the correct answers. The...
4
by: HxRLxY | last post by:
I am having a compile-time problem with a simple program I am writing. When I attempt to compile, I get the error "non-static variable this cannot be referenced from a static context". The error...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.