Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Assignment: Display Character

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Assignment: Display Character

    The example given is for number how can i change it to letters? Our assignment is pretty simple but i can't find it on the book. Im stuck

    “Read” in characters and display the character until the “z” character is entered (“read”)

    /* Guess a number between 1 and 10
       Anderson, Franceschi
    */
     
    import java.util.Random;
    import java.util.Scanner;
     
    public class GuessANumber
    {
      public static void main( String [ ] args )
      {
        Random random = new Random( );
        int secretNumber = random.nextInt( 10 ) + 1;
     
        Scanner scan = new Scanner( System.in );
     
        System.out.print( "I'm thinking of a number"
                    + " between 1 and 10. What is your guess? " );
        int guess = scan.nextInt( );
     
        if ( guess < 1 || guess > 10 )
        {
           System.out.println( "Well, if you're not going to try,"
                               + " I'm not playing." );
        }
        else
        {
           if ( guess == secretNumber )
               System.out.println( "Hoorah. You win!" ); 
           else
           {
               System.out.println( "The number was " + secretNumber );
     
               if ( Math.abs( guess - secretNumber ) > 3 )
                   System.out.println( "You missed it by a mile!" );
               else
                   System.out.println( "You were close." );
     
               System.out.println( "Better luck next time." );
           }
        }
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Assignment: Display Character

    how can i change it to letters
    Read the API doc for the Scanner class. It has several methods for reading different types of data.
    Java Platform SE 6

Similar Threads

  1. [SOLVED] Unknown Character
    By aussiemcgr in forum Java Theory & Questions
    Replies: 19
    Last Post: September 1st, 2010, 05:22 PM
  2. [SOLVED] last character position
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 05:31 AM
  3. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  4. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM
  5. Java program to display single and plural words
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 10th, 2009, 11:09 PM