Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2016
    Posts
    2
    Rep Power
    0

    Default Trying to find if there are a certain amount of letters in a word.

    Hey, thanks for viewing this post. I'm stuck on the below bit as i'm trying to get it so if the username that the user enter is less than 6 letters, it will enter a loop and the user has 3 turns to make it larger before exiting. I can't get it so if it's < 6 letters, it enters it (I don't know the code for it. When I type in, e.g ("abc")it exits it with a red error msg). I've tried looking up results on other forums and indeed on this one but I am unsuccessful. If you have any ideas please reply i'm stuck for answers at this point. Thanks...



    Java Code:
    
    package ca2;
    import java.util.Scanner;
     
    
    
     
    public class CA2 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
           String userName;
           String password, password2;
           String strIn= "";
           
           int chances =3;
           int menuOption;
           String studentName, studentNumber;
           char xNumber;
           int softwareDevelopment ;
           int mathematics;
           int learningToLearn;
           int computerArchitecture;
           int userAttempts;
           int sixLetters;
           int attempts=3;
           
           
           
           
           
           System.out.println("Please enter a Username, No spaces.");
           userName = in.next();
           sixLetters = userName.charAt(6);
          if(sixLetters ==(' ')){
              userAttempts=0;
              
          
               while (userAttempts <3) {
                   
                   System.out.println("Username needs to be greater than 6 letters, Re-enter. "+attempts + " chances remaining");
                   userName = in.next();
                   attempts--;
                   userAttempts++;
                   
               }}
          else{
                       System.out.println("This will be shown after loop");
                       }
    Last edited by Mr_AfroChicken; 11-09-2016 at 10:32 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Trying to find if there are a certain amount of letters in a word.

    if the username that the user enter is less than 6 letters,
    Look at the API doc for the String class. It has a method that returns the length of the String.
    Java Platform SE 8

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Nov 2016
    Posts
    2
    Rep Power
    0

    Default Re: Trying to find if there are a certain amount of letters in a word.

    Ok, thanks. I'm still confused on what line of code to put in what variables though :/
    Last edited by Mr_AfroChicken; 11-09-2016 at 10:29 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Trying to find if there are a certain amount of letters in a word.

    You say you get a red error message.
    What is that message?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trying to find if there are a certain amount of letters in a word.

    Let me guess. IndexOutOfBoundsException at line 36. If your input string is less that 7 characters, userName.charAt(6)
    will throw an exception.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Trying to find if there are a certain amount of letters in a word.

    That would be my guess too.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. find website name by key word
    By Narayanan in forum New To Java
    Replies: 1
    Last Post: 10-27-2013, 12:55 PM
  2. Replies: 5
    Last Post: 10-11-2013, 12:31 AM
  3. Can StreamTokenizer find numbers followed by letters
    By kbreitsprecher in forum New To Java
    Replies: 1
    Last Post: 03-08-2012, 10:45 PM
  4. Replies: 3
    Last Post: 08-30-2011, 04:44 PM
  5. Find index position of every word in a String
    By pentace in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 08:26 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
  •