Results 1 to 2 of 2
Thread: Reentering a Loop
- 11-19-2008, 09:46 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Reentering a Loop
Alright, this is...a program that is in the labbook and should I'm sure be much easier than it is.
My teacher and I got it to the point where it is now, and what it seems to be doing is its not Reentering the loop. inside the loop. You give it a character, and what its SUPPOSED to do is search for it in multiple sentences until you tell it no you don't want to do it again.
BUT it goes into the first one, and it does that one right, then asks you for another go and re-initializes time to 0 for the next count up. but it wont reenter the counting loop.
Help will be appreciated. I'm sure its something small so...yea
The Code
java.util.*;
public class ChCount2
{
static Scanner console = new Scanner(System.in);
public static void main(String []args)
{
char ch, test;
int time, length, i;
String str, q;
i=0;
time=0;
test='y';
System.out.println("This program will take a letter" +
"and will locate how many times it's in a sentance.");
System.out.println("Please enter a letter.");
ch=console.nextLine().charAt(0);
while (test == 'y')
{
System.out.println("Please enter a sentence.");
str=console.nextLine();
while (i<str.length())
{ if (str.charAt(i)==ch)
time++;
i++;
}
System.out.println("The letter " + ch + "\n"
+ "Appears " + time + " times in the sentence \n"
+ str);
System.out.println("Would you like to enter another sentence?" + "y/n");
q=console.nextLine();
test=q.charAt(0);
time=0;
}
}
}
- 11-19-2008, 10:40 PM #2
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Similar Threads
-
While loop
By sjhentges in forum New To JavaReplies: 11Last Post: 11-04-2008, 04:26 PM -
For Loop
By kian_hong2000 in forum New To JavaReplies: 1Last Post: 08-07-2008, 02:01 PM -
do...while loop
By eva in forum New To JavaReplies: 16Last Post: 01-31-2008, 06:44 AM -
while loop
By michcio in forum New To JavaReplies: 5Last Post: 01-27-2008, 12:56 AM -
While loop
By leebee in forum New To JavaReplies: 1Last Post: 07-18-2007, 03:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks