0

I recently posted about a school project im working on and received a lot of help. Im still working on this and I've encountered a new issue: I ask the user to input the name of a school subject, but I need it to be from 1 to 15 characters max, if he were to input more than 15 characters, the result I want to print would not be what I expect as I already have thought of the design. Tried using if(mystringarray[arrayname]>15) but I know this is meant for Integers. Any help would be appreciated, im willing to learn.

    //---------------------------------------------------------------------------
    String arrayer[] = new String[z];
    for(multi=0; multi<arrayer.length; multi++){

            do{
            out.print("\n");
            out.print("Introduzca el nombre de la materia "+(multi+1)+" : ");
            try{
                arrayer[multi]=LeerTeclado.nextLine();  
                if(arrayer[multi]>15){
                    out.println ("\nSubject name should be shorter than 15 characters\n");
                    continue;
                }
                break;
            }catch (InputMismatchException ex) {
                out.println("Subject name should be shorter than 15 characters\n");
                LeerTeclado.next();
            }
        }while(true);   



    }

    //---------------------------------------------------------------------------    
  • [`String`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html) class has [`length()`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#length()) method. – PM 77-1 Nov 27 '14 at 04:30

3 Answers3

1

String has a method for fetching length use

if(arrayer[multi].length() > 15)
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Ravi Teja N
  • 145
  • 2
  • 12
0

You just need to ask for the length of the String

if(arrayer[multi].length() > 15)
isomarcte
  • 1,981
  • 12
  • 16
0

Use arrayer[multi].length() to get the length of the String

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Deva
  • 11
  • 2