Question

How do I write a java code that mimics charAt without using java API just primitives...

How do I write a java code that mimics charAt without using java API just primitives and no charAt to be used? I know it comes from primitives but I am confused on how to assemble the loops to derive my own charAt code

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class StringCharAt {

    public static char charAtIndex(String s, int index) {
        if (index < 0 || index >= s.length()) {
            throw new IllegalArgumentException("Index is invalid");
        }
        return s.toCharArray()[index];
    }

    public static void main(String[] args) {
        System.out.println(charAtIndex("hello", 0));
        System.out.println(charAtIndex("hello", 1));
        System.out.println(charAtIndex("hello", 2));
        System.out.println(charAtIndex("hello", 3));
        System.out.println(charAtIndex("hello", 4));
    }
}
Add a comment
Know the answer?
Add Answer to:
How do I write a java code that mimics charAt without using java API just primitives...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi I need some help writing a security code using python mongodb restful api. I just...

    Hi I need some help writing a security code using python mongodb restful api. I just need help on how to provide a security login to user to enter their username and password or to have a block in using extra access once they have logined and they want more access to the databases they will be force to sign out of the server but I just do not know how to start on it can someone show me how...

  • How do I do this in Java without using the functional API? I can't use anything...

    How do I do this in Java without using the functional API? I can't use anything special; this is for APCS method header: public List noX(List strings) Functional-1 noX prev | next chance Given a list of strings, return a list where each string has all its "x" removed. Vi nox(["ax", "bb", "cx"]) ["a", "bb", "c"] nox(Тххах", "Xbxbx", "Xxcx"]) ["a", "bb", "c"]

  • In Java All methods listed below must be public and static. If your code is using...

    In Java All methods listed below must be public and static. If your code is using a loop to modify or create a string, you need to use the StringBuilder class from the API. Keep the loops simple but also efficient. Remember that you want each loop to do only one "task" while also avoiding unnecessary traversals of the data. No additional methods are needed. However, you may write additional private helper methods, but you still need to have efficient...

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm...

    Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm Write a static function called RemoveNullElements() without creating another array. that remove Null elements and shift all the rest of the elements to the left/front (small indexes) of the array without changing the size or length of the original array( this meant the front of the array will have all the not Null elements, and the other haft will be empty/Null. Please provide test...

  • I need help with this c++ code. This is my first computer science course. I have...

    I need help with this c++ code. This is my first computer science course. I have not learned arrays but I am learning vectors. I am also required to write this code without do loops or any other form of advanced coding technique. Please help me figure out this code using the basic coding techniques that you would find in an introduction to cs. Thank you for your help, I don't even know where to start :)

  • Write a section of Java code using while, do-while and for loops Write a section of...

    Write a section of Java code using while, do-while and for loops Write a section of Java code that will print out random integers between 1 and 100 while N is less than or equal to 5. Sample output > random # 1 is: 73 random # 2 is: 68 random # 3 is: 76 random # 4 is: 64

  • (JAVA) How do I access the values calculated in a for loop after the for loop...

    (JAVA) How do I access the values calculated in a for loop after the for loop is finished iterating? My task is to calculate subtotals using values from the user dependent on how many items they are buying. I used a nested for loop with if loops to calculate the subtotals based on what the price is (user input) and quantity of items (user input). I was able to get the loop to run how I need to; however, after...

  • how do i do this in basic java without using hashmat or anything complicated Two-Dimensional Arrays....

    how do i do this in basic java without using hashmat or anything complicated Two-Dimensional Arrays. Write the three functions described below. Demonstrate that your code works by calling each function and printing out the results. All of your code should be in one file. Upload your java code and a file containing the output of running your program to show that it works. Write a function called create2DIntArray that creates a two-dimensional array of integers between 0 and 10....

  • How do I Write a java code where it records the user inputs of strings ....

    How do I Write a java code where it records the user inputs of strings . <orderString>

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT