0

I hope the evening finds you well. My problem tonight is that I'm trying to create a char array, but I can't use any of the good ways to do it. I can only use String.length and String.charAt() my code is a miserable sad mess. This kind of combines all the things I've ever been terrible at so far.

Ultimately what I'll be trying to do is find a way to insert, delete, and replace things given certain indexes of a user-input string, but one step at a time, right?

This is what I have right now for my insert method, it's not even close to done obviously. All I'm really trying to figure out here, is how to print out my array, because when I try to print it out right now its just printing blanks. I apologize for my ineptitude.

 public String insert_text(String fromtest){


    System.out.println(fromtest.length());

    System.out.println(fromtest);
    for(int i=0;i<fromtest.length();i++){
        text=new char[fromtest.charAt(i)];
        System.out.println(text[i]);
    }

    System.out.println("* Enter the starting position:");
    int startpos=k.nextInt();

    System.out.println("* Enter the text you want to insert:");
    String instring=k.next();

    return fromtest;

}

I'm not sure why, but the prompt just say that the only things we can use are Sting.Length and String.charAt and store them to an array which I forgot to mentioned should be private char [] text.

4
  • Convert first you string in char array using toArray function. Commented Dec 10, 2013 at 5:36
  • new char[fromtest.charAt(i)] does not make any sense at all. You're using the int value of a character as the size of an array. Why? What are you trying to accomplish with that for loop? For that matter, what exactly is this entire method supposed to do? Commented Dec 10, 2013 at 5:39
  • This seems like homework? i'm guessing your not permitted to use the toCharArray() method? Commented Dec 10, 2013 at 5:39
  • You are correct, not allowed to use toCharArray(), it indeed is homework I swear I'm not looking for answers just guidance. Commented Dec 10, 2013 at 5:42

5 Answers 5

1

String.toCharArray() is the function that you are looking for. It converts the String into equivalent Char array.

Convert an input String to char[] as the following sample code:

public static void main(String args[])
{
 String str = "Sameer";
 char[] cArray = str.toCharArray();
 //Now perform functions with your charArray
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Why can't you use

char [] input = fromtest.toCharArray(); // get's the character array as called...

if you can't use toCharArray you could try

char [] array = new char[fromtest.length()];
for (int i = 0; i < fromtest.length(); i++) {
  array[i] = fromtest.charAt(i);
}

2 Comments

I'm not sure, that is just part of the parameter that I'm getting. I believe the goal is to learn more about Arrays and get a good grasp on them (it's homework). I've been trying hard for hours. I tried hitting it, I tried yelling at it, I even tried calling it names, but nothing has seemed to work so far.
You are my hero, that's kind of what I was going for, I just clearly did a terrible job. Thank you so much for your help. That syntax will help me so much moving forward.
0

You can print each char by :

System.out.println(fromtest.charAt(i));

1 Comment

Thank you very much, that will be quite useful.
0
         public String insert_text(String fromtest){


          Char[] charArray = fromtest.toCharArray();
           // do whateveryou want;   
          return fromtest;
   }

Comments

0

So far i understand your problem is you need to make dynamic array of what String is passed in this function,

use char[] charArray = string.toCharArray();

but what you are doing i prefer you stack and queues

1 Comment

Thats true, but it is my understanding that we are not allowed to use to.CharArray();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.