7 Replies - 4591 Views - Last Post: 27 February 2010 - 10:13 AM Rate Topic: -----

#1 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

loop exercises

Posted 26 February 2010 - 07:38 PM

hi,

im working on some java exercises to try to build some skill. the one that i am having problems with is pretty simple, but im having trouble.

im supposed to create an infinite loop and be able to stop it by entering a key on the keyboard.

what i have is this:

import java.util.Scanner;
public class InfiniteLoop
{
  public static void main(String[]args)
  {
    Scanner sc = new Scanner(System.in);
    
    int number = 0;
    
    System.out.println("Enter S to Stop");
    String line = sc.nextLine();
    char character = line.charAt(0);
    
    while(character != 'S')
    {
      System.out.println(number++);
    }
   
    
  }
  
}

    
    
    



if anyone has any help, thank you very much.

Is This A Good Question/Topic? 0
  • +

Replies To: loop exercises

#2 Riveted  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 87
  • Joined: 03-December 09

Re: loop exercises

Posted 26 February 2010 - 07:59 PM

The only problem that you have is that you need your structure to look like this:
char character = 'a';
    
    while(character != 'S')
    {
      System.out.println(number++);
      String line = sc.nextLine();
      character = line.charAt(0);
    }



If you turn that into a do...while() loop, you don't have to initialize character the first time, I believe.

This will also have the side-effect of you having to give it some kind of input every time the loop comes around. The only way that I can think of getting around that is with Action Listeners tied to the keyboard input (aka "Key Listeners"), but everything I'm reading is saying that you can't do that without at least some kind of minimal gui window.

edit: realized i didn't exactly fix the original problem :)

This post has been edited by Riveted: 26 February 2010 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: loop exercises

Posted 26 February 2010 - 08:29 PM

lol no problem, thanks for the response though. i got the problem from

http://www.jobsnake....penarticle=8533

it is #1

i was thinking that it wouldnt require much skill as it is listed as basics
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: loop exercises

Posted 26 February 2010 - 10:31 PM

That does not make sense
   while(character != 'S') 
    { 
      System.out.println(number++); 
    } 


A while loop into which you do not change the control variable, in your case: character
will loop forever if ever you enter into it
Was This Post Helpful? 0
  • +
  • -

#5 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: loop exercises

Posted 26 February 2010 - 10:37 PM

    
    System.out.println("Enter S to Stop"); // Prints to console
    String line = sc.nextLine(); // Sets String line equal to what sc.nextLine() returns
    char character = line.charAt(0); //Sets character equal to the character at index 0 of String line
    
    while(character != 'S') // This conditional statement will all ways be true unless you enter S as 
    {                       // the first character 
      System.out.println(number++); // Prints incrementing number to console
    }



You never allow character to be update. Therefore, unless you enter S as your first character, the while conditional statement is always true.

This post has been edited by redhotfire0: 26 February 2010 - 10:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: loop exercises

Posted 26 February 2010 - 11:47 PM

View Postredhotfire0, on 26 February 2010 - 09:37 PM, said:

    
    System.out.println("Enter S to Stop"); // Prints to console
    String line = sc.nextLine(); // Sets String line equal to what sc.nextLine() returns
    char character = line.charAt(0); //Sets character equal to the character at index 0 of String line
    
    while(character != 'S') // This conditional statement will all ways be true unless you enter S as 
    {                       // the first character 
      System.out.println(number++); // Prints incrementing number to console
    }



You never allow character to be update. Therefore, unless you enter S as your first character, the while conditional statement is always true.



...any advice?
Was This Post Helpful? 0
  • +
  • -

#7 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: loop exercises

Posted 27 February 2010 - 08:35 AM

View Postgibson.nathan, on 26 February 2010 - 10:47 PM, said:

...any advice?


What else have you tried? The only way I can think of doing two things at once, looping the incrementing integer, while looking for input would require, I think, the use of threads. If so, I can not provide any assistance because I know to little.
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,048
  • Joined: 15-July 08

Re: loop exercises

Posted 27 February 2010 - 10:13 AM

gibson.nathan, you grab input OUTSIDE of the the loop, therefore, once inside, there is NO way to break out of of the loop. What you need to do is to either make an input inside the loop, or to make KeyListeners, which require GUIs and that is getting a little complex. Even with constant input, the loop is still technically infinite until S is entered. Note that "S" must be capitalized or it won't exit.

    System.out.println("Enter S to Stop");    
    while(character != 'S') 
    {   

      String line = sc.nextLine(); 
      char character = line.charAt(0);
      System.out.println(number++); 
      
    }



Now, it will only input inside the loop, thus allowing it to quit.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1