Results 1 to 2 of 2
- 10-04-2011, 03:48 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
How to add characters that have integers stored in them
Write a Java program that may be part of a game, scoring points for words. It should:
1. Ask the user to type in a word, using a JOptionPane.showInputDialog().
2. Look at each letter in the word and add up the points for each letter. The points for letters are:
a. Vowels („a‟, „e‟, „i‟, „o‟ and „u‟) are valued at zero.
b. The letters „x‟ and „q‟ are valued at 5.
c. All other letters are valued at 1.
d. Anything not a letter should be ignored.
e. Upper case and lower case letters count the same.
3. Using a JOptionPane.showMessageDialog(), display the word and the score.
4. Repeat this process until the user types the word "Stop
This is what i have so far, it counts the letters inputed by the user instead of the stored number of the character, i need help fixing this problem thank youJava Code:import javax.swing.*; public class Project0 { public static void main(String[] args) { String insertWord; int sum=0; // This line asks the user for input insertWord = JOptionPane.showInputDialog(null,"Type in a Word: " ); char a,A,e,E,I,i,O,o,U,u=0; char x,X,q,Q=5; char b,B,C,c,D,d,F,f,G,g,H,h,J,j,K,k,L,l,m,M,N,n,P,p,R,r,S,s,T,t,V,v,W,w,y,Y,Z,z=1; //Count letters in word for(int ia = 0; ia<insertWord.length(); ia++){ if(Character.isLetter(insertWord.charAt(ia))) sum++;{ } } System.out.println("There are: " + sum + " sum"); } }
- 10-04-2011, 09:16 AM #2
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: How to add characters that have integers stored in them
Your code only counts the number of letters the user inputs, as you said. You want to test EACH letter and then depending on the letter, add 0,5 or 1 to 'sum'.
Java Code:for(int ia = 0; ia<insertWord.length(); ia++){ if (input == a ||e ||i ||o ||u) sum += 0; if (input == "x" ||"q") sum += 5; if input == a || b || c ||etc.) sum += 1; }
Similar Threads
-
prints characters in one case, but in another it returns integers. Why is that?
By bigsonny in forum New To JavaReplies: 16Last Post: 06-22-2011, 07:14 AM -
Convert integers into ACSII characters.
By Valkyrie in forum New To JavaReplies: 4Last Post: 11-24-2009, 02:39 AM -
stored procedure
By sankarigopi in forum JDBCReplies: 1Last Post: 11-13-2008, 04:53 PM -
Stored Procedures
By geeta_ravikanti in forum JDBCReplies: 1Last Post: 04-22-2008, 02:34 AM -
Convert some special characters stored in a MySql
By romina in forum JDBCReplies: 1Last Post: 08-07-2007, 05:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks