|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
New to java(have code) need very simple corrections
Hi, thanks for viewing my thread. I have this code 99% but im alittle stuck. The program suppose to read in a sentence and report back how many lower and upper case 'e' it has. But exits when user inputs "Stop" or "stop" and thats where I am stuck here is my code.
Code:
import javax.swing.JOptionPane;
public class firstProject{
public static void main(String[] args){
while(true){
int lowerECount = 0, higherECount = 0;
String sentence = JOptionPane.showInputDialog(null, "Enter a sentence");
if(sentence == "Stop" || sentence == "stop"){
System.exit(0);
}
for(int i = 0; i < sentence.length(); i++){
if(sentence.charAt(i) == 'e'){
lowerECount++;
}
if(sentence.charAt(i) == 'E'){
higherECount++;
}
}//for
JOptionPane.showMessageDialog(null, "number of lower case e's are: " + lowerECount +
" " + "number of higher case e's are: " + higherECount);
}//while
}//main
}//class
|
|
#2
|
||||
|
||||
|
You might want to consider switching the code around.
I haven't tested this, and it might be wrong, but try playing with this for a bit. Code:
String sentence = new String("");
while(!sentence.equals("Stop")){
int lowerECount = 0, higherECount = 0;
sentence = JOptionPane.showInputDialog(null, "Enter a sentence");
// [ ... ]
}
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter! DevArticles Forum Moderator "The net is a waste of time, and that's exactly what's right about it." -- William Gibson |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > New to java(have code) need very simple corrections |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|