How to search String array through char array?
I want to build a simple dictionary search program without using dictionary library. I want to search the string array if the string in array is equal to the char array. It should print the string and its index number. If there are 2 strings that are matching the char then it should print the first string and leave the after string
.e.g String array["fine","rest","door","shine"] char character ['t','r','e','s']. the answer should be "rest" at index 1. and if the String rest is repeat then it should only print first one.
I tried to compare the string array and char array but its returning all the words of string array that matches char array.
String strArray=new String[4];
char chrArray=new char[4];
String value="";
char compare;
System.out.println("Enter the words :");
for(int i=0;i<strArray.length;i++){
strArray[i]=input.next();
}
System.out.println("Enter the Characters :");
for (int i = 0; i < chrArray.length; i++) {
chrArray[i]=input.next().charAt(0);
}
for (int i = 0; i < strArray.length; i++) {
if(strArray[i].length()==chrArray.length){
if(""+strArray[i]!=value){
value="";
}
for (int j = 0; j < strArray[i].length(); j++) {
for (int k = 0; k < chrArray.length; k++) {
if(strArray[i].charAt(j)==chrArray[k]){
value=value+strArray[i].charAt(j);
}
}
}
}
}
System.out.println(value);
The output should be the string from array that is equal to char array.
java arrays
add a comment |
I want to build a simple dictionary search program without using dictionary library. I want to search the string array if the string in array is equal to the char array. It should print the string and its index number. If there are 2 strings that are matching the char then it should print the first string and leave the after string
.e.g String array["fine","rest","door","shine"] char character ['t','r','e','s']. the answer should be "rest" at index 1. and if the String rest is repeat then it should only print first one.
I tried to compare the string array and char array but its returning all the words of string array that matches char array.
String strArray=new String[4];
char chrArray=new char[4];
String value="";
char compare;
System.out.println("Enter the words :");
for(int i=0;i<strArray.length;i++){
strArray[i]=input.next();
}
System.out.println("Enter the Characters :");
for (int i = 0; i < chrArray.length; i++) {
chrArray[i]=input.next().charAt(0);
}
for (int i = 0; i < strArray.length; i++) {
if(strArray[i].length()==chrArray.length){
if(""+strArray[i]!=value){
value="";
}
for (int j = 0; j < strArray[i].length(); j++) {
for (int k = 0; k < chrArray.length; k++) {
if(strArray[i].charAt(j)==chrArray[k]){
value=value+strArray[i].charAt(j);
}
}
}
}
}
System.out.println(value);
The output should be the string from array that is equal to char array.
java arrays
"if(""+strArray[i]!=value){
" isn't the correct way to compare strings. The+
is redundant (unless you expectstrArray[i]
to be null), and then you'd need to useequals
(eitherstrArray[i].equals(value)
, orObjects.equals(strArray[i], value)
if you think it might be null).
– Andy Turner
Jan 1 at 11:20
Format code correctly
– beso9595
Jan 1 at 11:22
I m a beginner and i don't know if my code is correct but if some one just write this program i will be very thankful.The code i want is described above.
– Ali
Jan 1 at 11:29
add a comment |
I want to build a simple dictionary search program without using dictionary library. I want to search the string array if the string in array is equal to the char array. It should print the string and its index number. If there are 2 strings that are matching the char then it should print the first string and leave the after string
.e.g String array["fine","rest","door","shine"] char character ['t','r','e','s']. the answer should be "rest" at index 1. and if the String rest is repeat then it should only print first one.
I tried to compare the string array and char array but its returning all the words of string array that matches char array.
String strArray=new String[4];
char chrArray=new char[4];
String value="";
char compare;
System.out.println("Enter the words :");
for(int i=0;i<strArray.length;i++){
strArray[i]=input.next();
}
System.out.println("Enter the Characters :");
for (int i = 0; i < chrArray.length; i++) {
chrArray[i]=input.next().charAt(0);
}
for (int i = 0; i < strArray.length; i++) {
if(strArray[i].length()==chrArray.length){
if(""+strArray[i]!=value){
value="";
}
for (int j = 0; j < strArray[i].length(); j++) {
for (int k = 0; k < chrArray.length; k++) {
if(strArray[i].charAt(j)==chrArray[k]){
value=value+strArray[i].charAt(j);
}
}
}
}
}
System.out.println(value);
The output should be the string from array that is equal to char array.
java arrays
I want to build a simple dictionary search program without using dictionary library. I want to search the string array if the string in array is equal to the char array. It should print the string and its index number. If there are 2 strings that are matching the char then it should print the first string and leave the after string
.e.g String array["fine","rest","door","shine"] char character ['t','r','e','s']. the answer should be "rest" at index 1. and if the String rest is repeat then it should only print first one.
I tried to compare the string array and char array but its returning all the words of string array that matches char array.
String strArray=new String[4];
char chrArray=new char[4];
String value="";
char compare;
System.out.println("Enter the words :");
for(int i=0;i<strArray.length;i++){
strArray[i]=input.next();
}
System.out.println("Enter the Characters :");
for (int i = 0; i < chrArray.length; i++) {
chrArray[i]=input.next().charAt(0);
}
for (int i = 0; i < strArray.length; i++) {
if(strArray[i].length()==chrArray.length){
if(""+strArray[i]!=value){
value="";
}
for (int j = 0; j < strArray[i].length(); j++) {
for (int k = 0; k < chrArray.length; k++) {
if(strArray[i].charAt(j)==chrArray[k]){
value=value+strArray[i].charAt(j);
}
}
}
}
}
System.out.println(value);
The output should be the string from array that is equal to char array.
java arrays
java arrays
edited Jan 1 at 11:35
Ali
asked Jan 1 at 11:18
AliAli
83
83
"if(""+strArray[i]!=value){
" isn't the correct way to compare strings. The+
is redundant (unless you expectstrArray[i]
to be null), and then you'd need to useequals
(eitherstrArray[i].equals(value)
, orObjects.equals(strArray[i], value)
if you think it might be null).
– Andy Turner
Jan 1 at 11:20
Format code correctly
– beso9595
Jan 1 at 11:22
I m a beginner and i don't know if my code is correct but if some one just write this program i will be very thankful.The code i want is described above.
– Ali
Jan 1 at 11:29
add a comment |
"if(""+strArray[i]!=value){
" isn't the correct way to compare strings. The+
is redundant (unless you expectstrArray[i]
to be null), and then you'd need to useequals
(eitherstrArray[i].equals(value)
, orObjects.equals(strArray[i], value)
if you think it might be null).
– Andy Turner
Jan 1 at 11:20
Format code correctly
– beso9595
Jan 1 at 11:22
I m a beginner and i don't know if my code is correct but if some one just write this program i will be very thankful.The code i want is described above.
– Ali
Jan 1 at 11:29
"
if(""+strArray[i]!=value){
" isn't the correct way to compare strings. The +
is redundant (unless you expect strArray[i]
to be null), and then you'd need to use equals
(either strArray[i].equals(value)
, or Objects.equals(strArray[i], value)
if you think it might be null).– Andy Turner
Jan 1 at 11:20
"
if(""+strArray[i]!=value){
" isn't the correct way to compare strings. The +
is redundant (unless you expect strArray[i]
to be null), and then you'd need to use equals
(either strArray[i].equals(value)
, or Objects.equals(strArray[i], value)
if you think it might be null).– Andy Turner
Jan 1 at 11:20
Format code correctly
– beso9595
Jan 1 at 11:22
Format code correctly
– beso9595
Jan 1 at 11:22
I m a beginner and i don't know if my code is correct but if some one just write this program i will be very thankful.The code i want is described above.
– Ali
Jan 1 at 11:29
I m a beginner and i don't know if my code is correct but if some one just write this program i will be very thankful.The code i want is described above.
– Ali
Jan 1 at 11:29
add a comment |
2 Answers
2
active
oldest
votes
You can sort char array and then compare it using Arrays.equal
. By sorting char array, there will be no need to use 2 for loops.
import java.util.Arrays;
import java.util.Scanner;
public class Bug {
public static void main(String args) {
String strArray=new String[4];
char chrArray=new char[4];
Scanner input = new Scanner(System.in);
System.out.println("Enter the words :");
for(int i=0;i<strArray.length;i++){
strArray[i]=input.next();
}
System.out.println("Enter the Characters :");
for (int i = 0; i < chrArray.length; i++) {
chrArray[i]=input.next().charAt(0);
}
Arrays.sort(chrArray);
for (int i = 0; i < strArray.length; i++) {
char x = strArray[i].toCharArray();
Arrays.sort(x);
if(Arrays.equals(chrArray, x))
{
System.out.println(strArray[i]);
break;
}
}
}
}
Thanks u Mukesh for your code ,i was trying this from 2 days.God Bless You.
– Ali
Jan 1 at 12:23
Happy to help. Welcome @Ali
– Mukesh prajapati
Jan 1 at 12:27
add a comment |
You could also create a Map<Character, Integer>
to store counts for both the words and the characters, and compare each word character counts with the character counts. If both are equal, print it out.
Demo:
import java.util.Map;
import java.util.HashMap;
public class Example {
private static final String words = {"fine", "rest", "door", "shine"};
private static final char chars = {'t', 'r', 'e', 's'};
/**
* Initialise String character counts in a hashmap datastructure.
* @param word The string word to iterate.
* @return Hashmap of character -> counts.
*/
private static Map<Character, Integer> getCharCounts(String word) {
Map<Character, Integer> wordCounts = new HashMap<>();
for (int i = 0; i < word.length(); i++) {
Character character = word.charAt(i);
// If key doesn't exist, initialise it
if (!wordCounts.containsKey(character)) {
wordCounts.put(character, 0);
}
// Increment count by 1
wordCounts.put(character, wordCounts.get(character) + 1);
}
return wordCounts;
}
public static void main(String args) {
// Initialise character counts first
Map<Character, Integer> charCounts = getCharCounts(String.valueOf(chars));
for (int i = 0; i < words.length; i++) {
Map<Character, Integer> wordCounts = getCharCounts(words[i]);
// If Hashmaps are equal, then we have found a match
if (wordCounts.equals(charCounts)) {
System.out.println(words[i] + ", at index = " + i);
break;
}
}
}
}
Output:
rest, at index = 1
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994996%2fhow-to-search-string-array-through-char-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can sort char array and then compare it using Arrays.equal
. By sorting char array, there will be no need to use 2 for loops.
import java.util.Arrays;
import java.util.Scanner;
public class Bug {
public static void main(String args) {
String strArray=new String[4];
char chrArray=new char[4];
Scanner input = new Scanner(System.in);
System.out.println("Enter the words :");
for(int i=0;i<strArray.length;i++){
strArray[i]=input.next();
}
System.out.println("Enter the Characters :");
for (int i = 0; i < chrArray.length; i++) {
chrArray[i]=input.next().charAt(0);
}
Arrays.sort(chrArray);
for (int i = 0; i < strArray.length; i++) {
char x = strArray[i].toCharArray();
Arrays.sort(x);
if(Arrays.equals(chrArray, x))
{
System.out.println(strArray[i]);
break;
}
}
}
}
Thanks u Mukesh for your code ,i was trying this from 2 days.God Bless You.
– Ali
Jan 1 at 12:23
Happy to help. Welcome @Ali
– Mukesh prajapati
Jan 1 at 12:27
add a comment |
You can sort char array and then compare it using Arrays.equal
. By sorting char array, there will be no need to use 2 for loops.
import java.util.Arrays;
import java.util.Scanner;
public class Bug {
public static void main(String args) {
String strArray=new String[4];
char chrArray=new char[4];
Scanner input = new Scanner(System.in);
System.out.println("Enter the words :");
for(int i=0;i<strArray.length;i++){
strArray[i]=input.next();
}
System.out.println("Enter the Characters :");
for (int i = 0; i < chrArray.length; i++) {
chrArray[i]=input.next().charAt(0);
}
Arrays.sort(chrArray);
for (int i = 0; i < strArray.length; i++) {
char x = strArray[i].toCharArray();
Arrays.sort(x);
if(Arrays.equals(chrArray, x))
{
System.out.println(strArray[i]);
break;
}
}
}
}
Thanks u Mukesh for your code ,i was trying this from 2 days.God Bless You.
– Ali
Jan 1 at 12:23
Happy to help. Welcome @Ali
– Mukesh prajapati
Jan 1 at 12:27
add a comment |
You can sort char array and then compare it using Arrays.equal
. By sorting char array, there will be no need to use 2 for loops.
import java.util.Arrays;
import java.util.Scanner;
public class Bug {
public static void main(String args) {
String strArray=new String[4];
char chrArray=new char[4];
Scanner input = new Scanner(System.in);
System.out.println("Enter the words :");
for(int i=0;i<strArray.length;i++){
strArray[i]=input.next();
}
System.out.println("Enter the Characters :");
for (int i = 0; i < chrArray.length; i++) {
chrArray[i]=input.next().charAt(0);
}
Arrays.sort(chrArray);
for (int i = 0; i < strArray.length; i++) {
char x = strArray[i].toCharArray();
Arrays.sort(x);
if(Arrays.equals(chrArray, x))
{
System.out.println(strArray[i]);
break;
}
}
}
}
You can sort char array and then compare it using Arrays.equal
. By sorting char array, there will be no need to use 2 for loops.
import java.util.Arrays;
import java.util.Scanner;
public class Bug {
public static void main(String args) {
String strArray=new String[4];
char chrArray=new char[4];
Scanner input = new Scanner(System.in);
System.out.println("Enter the words :");
for(int i=0;i<strArray.length;i++){
strArray[i]=input.next();
}
System.out.println("Enter the Characters :");
for (int i = 0; i < chrArray.length; i++) {
chrArray[i]=input.next().charAt(0);
}
Arrays.sort(chrArray);
for (int i = 0; i < strArray.length; i++) {
char x = strArray[i].toCharArray();
Arrays.sort(x);
if(Arrays.equals(chrArray, x))
{
System.out.println(strArray[i]);
break;
}
}
}
}
answered Jan 1 at 11:39
Mukesh prajapatiMukesh prajapati
865418
865418
Thanks u Mukesh for your code ,i was trying this from 2 days.God Bless You.
– Ali
Jan 1 at 12:23
Happy to help. Welcome @Ali
– Mukesh prajapati
Jan 1 at 12:27
add a comment |
Thanks u Mukesh for your code ,i was trying this from 2 days.God Bless You.
– Ali
Jan 1 at 12:23
Happy to help. Welcome @Ali
– Mukesh prajapati
Jan 1 at 12:27
Thanks u Mukesh for your code ,i was trying this from 2 days.God Bless You.
– Ali
Jan 1 at 12:23
Thanks u Mukesh for your code ,i was trying this from 2 days.God Bless You.
– Ali
Jan 1 at 12:23
Happy to help. Welcome @Ali
– Mukesh prajapati
Jan 1 at 12:27
Happy to help. Welcome @Ali
– Mukesh prajapati
Jan 1 at 12:27
add a comment |
You could also create a Map<Character, Integer>
to store counts for both the words and the characters, and compare each word character counts with the character counts. If both are equal, print it out.
Demo:
import java.util.Map;
import java.util.HashMap;
public class Example {
private static final String words = {"fine", "rest", "door", "shine"};
private static final char chars = {'t', 'r', 'e', 's'};
/**
* Initialise String character counts in a hashmap datastructure.
* @param word The string word to iterate.
* @return Hashmap of character -> counts.
*/
private static Map<Character, Integer> getCharCounts(String word) {
Map<Character, Integer> wordCounts = new HashMap<>();
for (int i = 0; i < word.length(); i++) {
Character character = word.charAt(i);
// If key doesn't exist, initialise it
if (!wordCounts.containsKey(character)) {
wordCounts.put(character, 0);
}
// Increment count by 1
wordCounts.put(character, wordCounts.get(character) + 1);
}
return wordCounts;
}
public static void main(String args) {
// Initialise character counts first
Map<Character, Integer> charCounts = getCharCounts(String.valueOf(chars));
for (int i = 0; i < words.length; i++) {
Map<Character, Integer> wordCounts = getCharCounts(words[i]);
// If Hashmaps are equal, then we have found a match
if (wordCounts.equals(charCounts)) {
System.out.println(words[i] + ", at index = " + i);
break;
}
}
}
}
Output:
rest, at index = 1
add a comment |
You could also create a Map<Character, Integer>
to store counts for both the words and the characters, and compare each word character counts with the character counts. If both are equal, print it out.
Demo:
import java.util.Map;
import java.util.HashMap;
public class Example {
private static final String words = {"fine", "rest", "door", "shine"};
private static final char chars = {'t', 'r', 'e', 's'};
/**
* Initialise String character counts in a hashmap datastructure.
* @param word The string word to iterate.
* @return Hashmap of character -> counts.
*/
private static Map<Character, Integer> getCharCounts(String word) {
Map<Character, Integer> wordCounts = new HashMap<>();
for (int i = 0; i < word.length(); i++) {
Character character = word.charAt(i);
// If key doesn't exist, initialise it
if (!wordCounts.containsKey(character)) {
wordCounts.put(character, 0);
}
// Increment count by 1
wordCounts.put(character, wordCounts.get(character) + 1);
}
return wordCounts;
}
public static void main(String args) {
// Initialise character counts first
Map<Character, Integer> charCounts = getCharCounts(String.valueOf(chars));
for (int i = 0; i < words.length; i++) {
Map<Character, Integer> wordCounts = getCharCounts(words[i]);
// If Hashmaps are equal, then we have found a match
if (wordCounts.equals(charCounts)) {
System.out.println(words[i] + ", at index = " + i);
break;
}
}
}
}
Output:
rest, at index = 1
add a comment |
You could also create a Map<Character, Integer>
to store counts for both the words and the characters, and compare each word character counts with the character counts. If both are equal, print it out.
Demo:
import java.util.Map;
import java.util.HashMap;
public class Example {
private static final String words = {"fine", "rest", "door", "shine"};
private static final char chars = {'t', 'r', 'e', 's'};
/**
* Initialise String character counts in a hashmap datastructure.
* @param word The string word to iterate.
* @return Hashmap of character -> counts.
*/
private static Map<Character, Integer> getCharCounts(String word) {
Map<Character, Integer> wordCounts = new HashMap<>();
for (int i = 0; i < word.length(); i++) {
Character character = word.charAt(i);
// If key doesn't exist, initialise it
if (!wordCounts.containsKey(character)) {
wordCounts.put(character, 0);
}
// Increment count by 1
wordCounts.put(character, wordCounts.get(character) + 1);
}
return wordCounts;
}
public static void main(String args) {
// Initialise character counts first
Map<Character, Integer> charCounts = getCharCounts(String.valueOf(chars));
for (int i = 0; i < words.length; i++) {
Map<Character, Integer> wordCounts = getCharCounts(words[i]);
// If Hashmaps are equal, then we have found a match
if (wordCounts.equals(charCounts)) {
System.out.println(words[i] + ", at index = " + i);
break;
}
}
}
}
Output:
rest, at index = 1
You could also create a Map<Character, Integer>
to store counts for both the words and the characters, and compare each word character counts with the character counts. If both are equal, print it out.
Demo:
import java.util.Map;
import java.util.HashMap;
public class Example {
private static final String words = {"fine", "rest", "door", "shine"};
private static final char chars = {'t', 'r', 'e', 's'};
/**
* Initialise String character counts in a hashmap datastructure.
* @param word The string word to iterate.
* @return Hashmap of character -> counts.
*/
private static Map<Character, Integer> getCharCounts(String word) {
Map<Character, Integer> wordCounts = new HashMap<>();
for (int i = 0; i < word.length(); i++) {
Character character = word.charAt(i);
// If key doesn't exist, initialise it
if (!wordCounts.containsKey(character)) {
wordCounts.put(character, 0);
}
// Increment count by 1
wordCounts.put(character, wordCounts.get(character) + 1);
}
return wordCounts;
}
public static void main(String args) {
// Initialise character counts first
Map<Character, Integer> charCounts = getCharCounts(String.valueOf(chars));
for (int i = 0; i < words.length; i++) {
Map<Character, Integer> wordCounts = getCharCounts(words[i]);
// If Hashmaps are equal, then we have found a match
if (wordCounts.equals(charCounts)) {
System.out.println(words[i] + ", at index = " + i);
break;
}
}
}
}
Output:
rest, at index = 1
edited Jan 1 at 12:51
answered Jan 1 at 12:23
RoadRunnerRoadRunner
11.3k31341
11.3k31341
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994996%2fhow-to-search-string-array-through-char-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"
if(""+strArray[i]!=value){
" isn't the correct way to compare strings. The+
is redundant (unless you expectstrArray[i]
to be null), and then you'd need to useequals
(eitherstrArray[i].equals(value)
, orObjects.equals(strArray[i], value)
if you think it might be null).– Andy Turner
Jan 1 at 11:20
Format code correctly
– beso9595
Jan 1 at 11:22
I m a beginner and i don't know if my code is correct but if some one just write this program i will be very thankful.The code i want is described above.
– Ali
Jan 1 at 11:29