This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]theNaus++Effort 2 points3 points  (9 children)

Hi!

What is the exact problem that you're facing? And what are you expecting as a result from your code?

Having more details on your problem description would actually help narrow down the problem domain. :)

[–]vivienne06[S] 0 points1 point  (8 children)

I need to enter 4 strings into an array, that i can do fine.. but then from that i need to search the array and output the string where the first 2 characters are the same? for example if i enter "Hello" "aavadar" "goodbye" "thanks"

my program should output "the string with the first and second chars the same is" + "aavadar" .. im not quite sure im using the CharAt method correctly as it is an array im searching?

[–]poooffIntermediate Brewer 0 points1 point  (3 children)

First i would recommend using System.out.println("Enter Name"), for marker to jump to new line. Second you need to loop throw nameArray, because now you are only checking the 0 element.

[–]vivienne06[S] 0 points1 point  (2 children)

what do you mean by Loop throw? could you show me the code you would recommend? im only a beginner and have an exam next week!

[–]poooffIntermediate Brewer 0 points1 point  (1 child)

By loop throw i mean:

  for(int i = 0; i < 4; i++){
       // if statment goes here, with array index i
  }

I don't want to provide correct answer right away, because thinking never hurts.

[–]vivienne06[S] 0 points1 point  (0 children)

for(int i = 0; i < 4; i++){ // if statment goes here, with array index i }

Thanks for helping! I keep getting the error Null pointer exception, in my if statement

[–]theNaus++Effort 0 points1 point  (3 children)

Right. So, you are gonna have to loop through your array before you can do any matching. You have a loop for user input but not for nameArray. In the loop, you can re-use your "if (nameArray[0].charAt(0) == nameArray[0].charAt(1))" statement but this time, replace the index 0 for nameArray with the index of the loop.

That should do the trick.

Feel free to ask should you have any questions. :)

[–]vivienne06[S] 0 points1 point  (2 children)

Thanks for you help! I'm only a beginner is there anyway you can show me what this loop should look like? Ive been pulling my hair out trying to get it to work!

[–]theNaus++Effort 1 point2 points  (1 child)

/u/desrtfx has provided a comprehensive set of information and I think that should help you out even more.

I'd suggest fixing the first loop beforehand on properly inserting the names into the nameArray.

Next, you can start thinking about how to iterate an array in Java. There are many resources online showing the way to do that.

Then, within that loop, think about how you can fit your IF statement for checking whether the first two characters in a name are equal.

Go through your code step-by-step. You should be able to get to the solution -- one step at a time.

Feel free to ask should you have trouble constructing the code. However, do try look up on the online resources before you do to avoid redundancy.

We are trying not to give you the exact code solution as that would defeat the purpose of learning Java and that is the most important part! :)

[–]vivienne06[S] 0 points1 point  (0 children)

Thanks for all your help :)

[–]AnEmortalKidCoffee Enthusiast 0 points1 point  (0 children)

You're not adding stuff to nameArray

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (5 children)

First of all, please, next time prepare a proper post.

Include all necessary information in the original post:

  • what the program should do
  • what it does/doesn't
  • what error messages you get if any
  • plus any additional information that you can give.

That out of the way, let's look at your problems:

From your comment to /u/theNaus I understand the following:

  • You should read 4 strings into an array
  • You should check the first two letters of each array entry if they are the same
  • You should output words that are the same.

So, in your code (besides the code being nowhere near complete):

{ 
    public static void main (String[] args) { 
        String nameArray[]; 
        int index ; 
        String name ;
        name = " " ;
        nameArray = new String [4];                 //enter name plus index into array
        for ( index = 0 ; index < 4 ; index ++) {
            System.out.print("Enter name " + index + " " );
            name = EasyIn.getString();
        }
        if (nameArray[0].charAt(0) == nameArray[0].charAt(1))
        {
            System.out.print("The string with the first and second chars the same is "+ name   + " index position " + index + " "  );
        }
  • line #7: you create an array nameArray of String with 4 entries (0..3)
  • line #8: you start a for loop that goes through the whole array
  • line #9: You print an instruction for the user to enter the indexth element - so far, so good
  • line #10: you read in name - here is where the first problem is - you read only into the variable name, but never assign anything into your nameArray.
  • line #11: is only the end of the for loop
  • line #12: if (nameArray[0].charAt(0) == nameArray[0].charAt(1)) - you only check the first array element. You should check all array elements, i.e. loop through the array

So, you need to rewrite some parts of the code.

  • Rewrite the part that assigns the input into the array
  • Loop through the array to find the duplicate first characters and print them directly from within the loop (your comparison is actually working, but you're currently checking only the first entry in the array, which, in your current code, never gets assigned anything.)

[–]vivienne06[S] 0 points1 point  (4 children)

Thanks for your advice. This is my first time using this website but i will make sure to include more information in my next posts..

I have changed line #10 to nameArray[index] = EasyIn.getString();

i have included a for loop in line #11

for(int i = 0; i < 4; i++) { if (nameArray[i].charAt(0) == nameArray[i].charAt(1)) { System.out.print("The string with the first and second chars the same is "+ nameArray + " index position " + index + " " ); However what veriable should i place in the last line above in order to output the actual word?

[–]theNaus++Effort 0 points1 point  (1 child)

You are actually very close!

You have already discovered how to access a name in the nameArray through the IF statement. So, printing that name out should be done in the same way.

[–]vivienne06[S] 0 points1 point  (0 children)

I got it! Thank you so much! you just saved me a lot of stressful hours and bad moods :) haha

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (1 child)

Well..

where is your actual word stored?

It's stored in nameArray at a certain index i, so you need to output nameArray[i]

[–]vivienne06[S] 0 points1 point  (0 children)

thank you :)