Results 1 to 2 of 2
  1. #1
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default 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;
    }
    }
    }

  2. #2
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

Similar Threads

  1. While loop
    By sjhentges in forum New To Java
    Replies: 11
    Last Post: 11-04-2008, 05:26 PM
  2. For Loop
    By kian_hong2000 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 03:01 PM
  3. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 07:44 AM
  4. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 01:56 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 04:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •