We have an exercise and it asks to create a program that will ask the user to initialize 2 character arrays, then compare the value of the variables(or the character array).

My problem is with the initialization:
I will create 2 char Arrays, then.
How can I have the input of the user, be the content of the array.??

Well. I have no problem in comparing the arrays
So, what are the processes that i could use to make this program?

Thanks!

You actually want to read a string from the keyboard, and store it in a character array? Do you want to do that?

Read the input from the keyboard by using the Scanner class. Search for examples. As for the rest you need to provide some more information.
For example, will you have to enter each character after the other or you will need to enter one word and then take that word and put its characters into one char array?

@tux4life, uhmm.. right..
can i do that?

ok ok, lack of information, sorry for that...

i'll have to input characters and store it to the character arrays.
i'll have 2 char arrays, so, i will also have 2 input characters...

for example,
the program will ask me to enter the first set of Variables, and the inputs will be stored inside the array. then will ask me to input second set of variables..

If you want to take input like: hello world, and want this to be stored in a character array when the user presses the enter-key, then you can do something like this (other ways exist):

  • Use the Scanner class to read a line from the input. (a reference to a String object containing that line will be returned).
  • Convert the String to a character array, by calling its toCharArray() method, and assigning the returned reference to a char[] reference variable.

@tux4life, uhmm.. right..
can i do that?

ok ok, lack of information, sorry for that...

i'll have to input characters and store it to the character arrays.
i'll have 2 char arrays, so, i will also have 2 input characters...

for example,
the program will ask me to enter the first set of Variables, and the inputs will be stored inside the array. then will ask me to input second set of variables..

Will you know how many characters to enter? If not, you need to have a while loop where the user enters input. When the users enters "quit" for example break from the while.
You can have the Scanner to read the input as a String and if it is not "quit" or if its length is 1 (characters have length 1), concatenate it to a String. Then convert that String to a character array.

uhm, you mean java.util.Scanner??
well, we can't use that... our practice is in java.io.* ... hahaha!!!

well, i have this code.

import java.io.*;
public class variable_input{
	public static void main (String argsp[])throws IOException{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		System.out.print("Enter First Variable: ");
		String input1 = in.readLine();
		char charinput1 = input1.charAt(0);
		char arrayinput1[] = {charinput1};

		System.out.print("Enter Second Variable: ");
		String input2 = in.readLine();
		char charinput2 = input2.charAt(0);
		char arrayinput2[] = {charinput2};

		if (arrayinput1 == arrayinput2)
			System.out.println("Equal");
		else
			System.out.println("Not Equal");

	}
}

it runs very fine, but when the program compares A (for the first variable) and A (for the second variable).

it says. NOT EQUAL.
how can i compare in if statement, two Characters??

>>it says. NOT EQUAL.
That's because you're comparing two reference variables. You're not actually comparing the values entered by the user.

arrayinput1, arrayinput2 are arrays. You need to loop the arrays and compare their values individually.

Also do you need to compare only 2 character variables or arrays that have many characters?

>how can i compare in if statement, two Characters??
To compare the first two characters entered, then you'll need to change your if-statement (line 16) to: if (arrayinput1[0] == arrayinput2[0]) >>You need to loop the arrays and compare their values individually.
Not necessarily, if he's allowed to use the java.util.Arrays class, then he can do the comparison using the equals() method.
Example:

// In this code example I assume that firstArray and secondArray are primitive array
// reference variables, and that and import java.util.Arrays; has been done.
if (Arrays.equals(firstArray, secondArray))
  System.out.println("The arrays are equal.");
else
  System.out.println("The arrays are not equal.");

ok ok, i changed my if statement to array[0] and so on.. so that the values in the arrays are being compared in my if statement.

and that is done...

THANKS GUYS!

Just to make sure you know this: that approach doesn't compare the whole array against another. It just compares two first elements of two arrays.

Ok, Thank you for the reminder :)

// In this code example I assume that firstArray and secondArray are primitive array
// reference variables, and that and import java.util.Arrays; has been done.
if (Arrays.equals(firstArray, secondArray))
System.out.println("The arrays are equal.");
else
System.out.println("The arrays are not equal.");

Hmm, What exactly is that testing though? Is it testing if the arrays are equal in size? contents are equal? or if they are the same type of array? I've never seen this before

>>Hmm, What exactly is that testing though? Is it testing if the arrays are equal in size? contents are equal? or if they are the same type of array? I've never seen this before

From the Java API - Arrays.equals(char[] a, char[] a2):

``Returns true if the two specified arrays of chars are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.'

i have a question..

what if, i have many input characters??

how can i do that?

>>what if, i have many input characters??

You want to compare two arrays? Check this:

// In this code example I assume that firstArray and secondArray are primitive array
// reference variables, and that and import java.util.Arrays; has been done.
if (Arrays.equals(firstArray, secondArray))
  System.out.println("The arrays are equal.");
else
  System.out.println("The arrays are not equal.");

However, if you're not allowed to use java.util.Arrays class, then you'll have to manually loop through both arrays as suggested by javaAddict.

oh yeah!!!
our instructor said, we can use the class (java.util.Arrays) .. and
that's it!
i'm fine :)
thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.