简体   繁体   中英

First and Last Character of a String

This lab we are doing in class is giving me a lot of trouble, and I wanted someone to help explain it to me. My teacher is out of town this weekend and so he can't answer my questions. The point of this lab is to find the first and last letter of each string. We also have to use "return" methods, but I am confused as to how they should be used. I know that I have to use "charAt();", but i'm not sure where to put this and how to test certain strings from another method.

import static java.lang.System.*;

public class firstandlast
{
    public String s, word, getFirst, getLast;

    public firstandlast(String s)
    {
    }

    public void setString(String s)
    {
    }

    public String getFirst()
    {
        return "";
    }

    public String getLast()
    {
        return "";
    }

    public String toString()
    {
        String output = "";
        return output;
    }
}

Code that tests the main class:

import static java.lang.System.*;

public class firstrunner
{
    public static void main ( String[] args )
    {
        firstandlast demo = new firstandlast("Hello");
        demo.setString("Hello");
        System.out.println( "first letter :: " + demo.getFirst() );
        System.out.println( "last letter :: " + demo.getLast() );

        demo.setString("World");
        System.out.println( "first letter :: " + demo.getFirst() );
        System.out.println( "last letter :: " + demo.getLast() );

        //add more test cases   

    }
}

I appreciate any help!

I'll give you a couple hints:

  1. charAt is a method belonging to objects of type String, meaning that the dot and the method name go after the variable name (eg yourString.extensionMethod() )

  2. When you use charAt, you're basically treating the string like an array of char objects. What would you do to get the first element in an array? What would you do to get the last?

There are plenty of pages on StackOverflow that'll tell you exactly how to do this.

Try This:

public class FirstAndLast {
  private String message;

  public FirstAndLast(String message) {
    this.message=message;
  }

  public char getFirst(){
    return message.charAt(0);
  }

  public char getLast(){
    int len=message.length();
    return message.charAt(len-1);
  }

  public static void main(String[] args) {
    FirstAndLast fl=new FirstAndLast("Here you go");
    System.out.println("First Char is "+fl.getFirst());
    System.out.println("First Char is "+fl.getLast());
  }
}
Scanner s=new Scanner(System.in);

System.out.println("Enter a word longer than 2 characters: ");
String word=s.next();
char a = word.charAt(0);
System.out.println(a);

int lastNum = word.length();
int x=lastNum-1;
char z = word.charAt(x);
System.out.println(z);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM