I would like to compare two user-defined strings and output the count of the number of characters shared between the two strings, without resorting to using arrays. I then need to output each of those characters. I understand the user-input part using a Scanner, but afterwards I am clueless.
For example, "hamper" as string1, and "happened" as string2 would return:
number of shared characters = 5
shared characters >> "h", "a", "p", "p", "e", "e"
Here is what i have so far. It prints each character on a separate line though. Is there a way without arrays to list them all on one line like above?:
public class CountMatches {
public static void main(String[] args)
{
//Declare both Strings.
String word1;
String word2;
int count = 0;
//Call for User Input.
Scanner inputDevice = new Scanner(System.in);
System.out.print("Input String 1 >> ");
word1 = inputDevice.next();
System.out.print("Input String 2 >> ");
word2 = inputDevice.next();
inputDevice.close();
//Determine lengths and set label accordingly.
String BigWord;
String SmallWord;
if (word1.length() > word2.length())
{
BigWord = word1;
SmallWord = word2;
}
else
{
BigWord = word2;
SmallWord = word1;
}
//Count and Display the like characters.
for (int i = 0; i < SmallWord.length(); i++)
{
if (BigWord.contains(String.valueOf(SmallWord.charAt(i))))
{
System.out.println("both words contain the letter " + SmallWord.charAt(i));
count++;
}
}
//Display the count of like characters.
System.out.print("Number of like characters >> " + count);
}
}
Let's say you have word1 and word2:
String biggerWord;
String smallerWord;
if (word1.length() > word2.length()) {
biggerWord = word1;
smallerWord = word2;
} else {
biggerWord = word2;
smallerWord = word1;
}
for (int i = 0; i < smallerWord.length(); i++) {
if (biggerWord.contains(String.valueOf(smallerWord.charAt(i)))) {
counter++;
}
}
This figures out which word is bigger. Then for the length of smallerWord, iterate over it one character at a time and see if biggerWord contains that character. If it does, increment the counter.
counter should then have the number of common characters by the end of the loop.
This was written freehand, so be aware of syntax and minor logic errors. Or I misunderstood your assignment. It should be pretty close though.
A really nice way is to sort the string alphabetically.
sortedWord1 = new String(Arrays.sort(word1.toCharArray()));
sortedWord2 = new String(Arrays.sort(word2.toCharArray()));
What that does is turn the words into character arrays, sort them alphabetically, then makes them into a string again.
The next step is just to iterate from the beginning and print out all common characters. This would be easier since they're sorted.
int index1 = 0;
int index2 = 0;
while((index1 < sortedWord1.length()) && (index2 < sortedWord2.length()) {
if(sortedWord1.charAt(index1) == sortedWord2.charAt(index2)) {
System.out.print(sortedWord1.charAt(index1) + " ");
index1++; index2++;
}
else if(sortedWord1.charAt(index1)> sortedWord2.charAt(index2)) {
index2++;
}
else {
index1++;
}
}
I haven't checked it for syntax errors, but it should be good.
Related
This project is used to identify whether or not a user's input is a palindrome, and if it's not, identifies how many characters don't match and their positions in the string (i.e characters 2 and 4 don't match). I've been able to figure out how to identify whether or not a string is a palindrome, but I'm struggling with how to specifically identify the characters that don't match in a non-palindrome. Here's my code so far:
import java.util.Scanner;
public class Palindrome
{
public static void main(String[] args)
{
String stringInput = "";
String inputReverse = "";
boolean isPalindrome = true;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
stringInput = keyboard.nextLine();
int stringLength = stringInput.length();
for(int i = stringLength - 1; i >=0; i--)
{
inputReverse = inputReverse + stringInput.charAt(i);
}
if(stringInput.equals(inputReverse))
{
System.out.println(stringInput + " is a valid palindrome.");
}
else
{
System.out.println(stringInput + " is not a valid palindrome.");
}
}
}
the output I want for when a string is not a palindrome is:
"The characters at index 0 and 3 do not match.
goop is not a valid palindrome.
number of invalid character matches: 1 "
I tried to use stringInput.charAt(0) but the user input is unpredictable, so I wouldn't be able to use char 0,1,2,3 etc forever. Any help?
Iterate from both ends of the string, moving toward the center and checking the corresponding characters each time.
int nomatch = 0;
for (int i = 0, j = stringLength - 1; i < j; i++, j--) {
if (stringInput.charAt(i) != stringInput.charAt(j)) {
++nomatch;
System.out.format("The characters at index %d and %d do not match.%n", i, j);
}
}
if (nomatch == 0) System.out.println(stringInput + " is a palindrome.");
else System.out.println(stringInput + " is not a palindrome. Number of invalid character matches: " + nomatch);
As this is home work, I'll only give general hints:
an easy way to reverse a string is inputReverse = new StringBuilder(stringInput).reverse().toString();
you only need to compare each character of the first half of the input with its reverse
use a for loop of int from 0 to half the length and pass it to charAt() for both strings and compare using ==
store the indexes of differences in a List<Integer>
So, I'm meant to get two version of a word given by user input, a version with just the lower case letters and then a version with just the upper case letters. Im then meant to find out if both of the words are palindromes. For example if the word was 'HEllO', the words HEO and ll would be created and then the output "HEO is not a palindrome, ll is a palindrome" would be printed. Im pretty sure my code makes sense but it won't say if either version of the original word is a palindrome. The following is the code.
public class comp1
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String lower = lowerCaseLetters(input);
String upper = upperCaseLetters(input);
palindromeUpper(upper);
palindromeLower(lower);
}
public static String lowerCaseLetters(String input)
{
char[] ar = new char[input.length()];
for(int i = 0; i < input.length(); i++)
{
if(Character.isLowerCase(input.charAt(i)))
{
ar[i] = input.charAt(i);
}
}
String lowercase = new String(ar);
return lowercase;
}
public static String upperCaseLetters(String input)
{
char[] ar = new char[input.length()];
for(int i = 0; i < input.length(); i++)
{
if(Character.isUpperCase(input.charAt(i)))
{
ar[i] = input.charAt(i);
}
}
String uppercase = new String(ar);
return uppercase;
}
public static void palindromeUpper(String sent)
{
String reverse = "";
for(int i = sent.length()-1; i >= 0; i--)
{
reverse += sent.charAt(i);
}
if(sent.equals(reverse))
{
System.out.println("Upper case " + sent + " is a palindrome");
}
else
{
System.out.println("Upper case " + sent + " is not a palindrome");
}
}
public static void palindromeLower(String sent)
{
String reverse = "";
for(int i = sent.length()-1; i >= 0; i--)
{
reverse += sent.charAt(i);
}
if(sent.equals(reverse))
{
System.out.println("Lower case " + sent + " is a palindrome");
}
else
{
System.out.println("Lower case " + sent + " is not a palindrome");
}
}
}```
arrays don't skip 'empty' values. In fact, there's no such thing as an empty slot; char[] ar = new char[input.length()]; gives you a char array with length slots, and each slot is filled with an actual character: The NUL character.
You then copy over all lowercase letters to their appropriate position, which means all non-lowercase letters still have the NUL character. You then turn this back into a string, which will be a string with lots of NUL characters, and those NUL characters will then prevent the rest of your code from working properly; ll is a palindrome, sure. but \0\0ll\0 is not.
You can't resize arrays, you need to create them at the right size. Thus, you have two options:
loop through the string twice. First time, you just count lowercase letters, that is all you do. Second time, you copy over the lowercase letters, and not to position i, but to a counter you maintain (the first l occurs at i=2, but needs to go into ar[0]. The second l occurs at i=3, and needs to go into ar[1]).
Don't use a char array; use a StringBuilder instead, which grows on demand. just .append any lowercase letters.
2 is probably easier, but involves the use of another class (java.lang.StringBuilder).
Note then that your palindromeLower and palindromeUpper methods are identical, other than the messaging.
My code checks if a certain number of user inputted string have any repeated characters. For example, if I input the strings "google" "paper" and "water", the code returns "paper" and "water"; because "google" has two Os.
I have the code part down, but when printing, a space appears after the very last string that is output and I can't figure out how to get rid of it.
import java.util.Scanner;
import java.util.*;
class words{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number or words: ");
String[] words = new String[sc.nextInt()];
System.out.print("Enter the strings: ");
boolean truth = false;
for (int i = 0; i < words.length; i++) {
words[i] = sc.next();
}
for(int i=0;i<words.length;i++){
int j;
for(j=1;j<words[i].length();j++) {
if(words[i].charAt(j) == words[i].charAt(j-1)){
break;
}
}
if(j==words[i].length()){
truth = true;
System.out.print(words[i]+" ");
}
}
if(!truth){
System.out.println("NONE");
}
}
}
Functions Make Logic Readable
Move the logic to check for repeating characters into a function; I would take advantage of String.toCharArray() and the shorter array syntax. Like,
private static boolean repeatedChars(String s) {
if (s == null) {
return false;
}
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length - 1; i++) {
if (chars[i] == chars[i + 1]) {
return true;
}
}
return false;
}
Then, you can use a lambda to filter your words based on them not having repeated characters and collect with Collectors.joining(CharSequence) like
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number or words: ");
String[] words = new String[sc.nextInt()];
System.out.print("Enter the strings: ");
for (int i = 0; i < words.length; i++) {
words[i] = sc.next();
}
System.out.println(Arrays.stream(words).filter(s -> !repeatedChars(s))
.collect(Collectors.joining(" ")));
And, if you need to display the NONE message you might re-use the Predicate<String> like
Predicate<String> pred = s -> !repeatedChars(s);
if (Arrays.stream(words).anyMatch(pred)) {
System.out.println(Arrays.stream(words).filter(pred).collect(Collectors.joining(" ")));
} else {
System.out.println("NONE");
}
There is an easy workaround for your problem. Instead of printing every word immediately if it does not have any continuous repetition, add it to a String variable with space at the end so that each word is separated by a space. After you run through your loop, you check if your flag is false and print NONE if it is false. If it is true, however, print the result string where you added everything with a .trim() at the end.
for (int i = 0; i < words.length; i++) {
words[i] = sc.next();
}
String result = ""; /*This is the string that holds all the strings that you need to print.*/
for(int i=0;i<words.length;i++){
int j;
for(j=1;j<words[i].length();j++) {
if(words[i].charAt(j) == words[i].charAt(j-1)){
break;
}
}
if(j==words[i].length()){
truth = true;
result = result + (words[i]+" ");
}
}
if(!truth){
System.out.println("NONE");
}
else{
System.out.println(result.trim()); /*The trim function removes any redundant space in the beginning and the end of the string.*/
}
Of-course doing it this way will waste a lot of Heap Memory but I guess this is for a small learning project. However, do look into StringBuilder on how to use it to avoid creating a lot of memory in the Heap!
I would like to split this input: 12132002(177) 012(207.5) 014(184) into two arrays, like this:
num[500] = 12132002,012,014, etc.
and
mark[500] = 177,207.5,184, etc.
The Fact is that I'm accepting values from user like this, where i don't know the total number which he/she will input.
How can I code in Java that kind of splitting? Is it like this?
int number[500];
for(int i=0;i<500;i++) {
number=num.split("//(");
}
To code "that kind of splitting", you will have to:
Declare your variables: String[] number, String[] mark, String num, and String[] numSplit.
Split num by " " (spaces). Assign this to numSplit.
Loop through numSplit from 0 to numSplit.length with i.
Set number[i] to numSplit[i] from the beginning to the first occurrence of a "(".
Set mark[i] to numSplit[i] from one character after the first occurrence of "(" to one character before the end.
Output number and mark
The full code:
String[] number = new String[500]; //1
String[] mark = new String[500];
String num = "12132002(177) 012(207.5) 014(184)";
String[] numSplit = num.split(" "); //2
for(int i = 0; i < numSplit.length; i++) { //3
number[i] = numSplit[i].substring(0, numSplit[i].indexOf("(")); //4
mark[i] = numSplit[i].substring(numSplit[i].indexOf("(") + 1, numSplit[i].length() - 1); //5
}
for(int i = 0; i < number.length; i++) System.out.println(number[i]); //6
for(int i = 0; i < mark.length; i++) System.out.println(mark[i]);
Which outputs:
12132002
012
014
null (x497)
177
207.5
184
null (x497)
Notice that number, mark, and numSplit are String arrays because the leading zeros would be cut off in not otherwise. If you don't mind the leading zeros being cut off then you can change num to an int array and mark to a double array (Because of the decimal in 207.5).
Ok buddy, this could be a solution for your problem. I chose to use the methods I have already created for some other project, but I think those can fit for this purpose as well, instead of using some complex REGEX expression. The output is good, though you have to figure out the way you want to store num and mark variables (I suggest arrays). Hope I helped.
public class Example {
public static void main(String[] args) {
String s = "12132002(177)012(207.5)014(184)";
// output 12132002,012,014 && 177,207.5,184
// it works good with this string as well -> s = "12132002(177)012(207.5)014(184)111(024)";
int numOfParanthesis = numOfParanthesis(s, '(');
String num = "";
String mark = "";
// array which contains positions of (
int[] indexesOpening = indexes(s, '(');
// array which contains positions of )
int[] indexesClosing = indexes(s, ')');
// logic
for(int i = 0; i < numOfParanthesis; i++){
if(i == 0){
num = s.substring(i, indexesOpening[i])+",";
mark = s.substring(indexesOpening[i]+1,indexesClosing[i])+",";
}else if(i!=numOfParanthesis-1){
num += s.substring(indexesClosing[i-1]+1, indexesOpening[i])+",";
mark += s.substring(indexesOpening[i]+1, indexesClosing[i])+",";
}else{
num += s.substring(indexesClosing[i-1]+1, indexesOpening[i]);
mark += s.substring(indexesOpening[i]+1, indexesClosing[i]);
}
}
System.out.println(num);
System.out.println(mark);
}
// returns array of positions for the given character
public static int[] indexes(String s, char c){
int numOfParanthesis = numOfParanthesis(s, c);
int[] indexes = new int[numOfParanthesis];
int delimetar = s.indexOf(c);
for(int i = 0; i < numOfParanthesis; i++){
if(i != -1){
indexes[i] = delimetar;
}
delimetar = s.indexOf(c, delimetar+1);
}
return indexes;
}
// returns how many times a character repeats in a string
public static int numOfParanthesis(String s, char c){
int number = s.indexOf(c);
int i = 0;
while (number >= 0){
number = s.indexOf(c, number+1);
i++;
}
return i;
}
}
Try this:
public class Test {
public static void main(String[] args) {
// Guess this is a string since it is a mix of integers
// and non-integers, characters like '(', ')' and space.
String str = "12132002(177) 012(207.5) 014(184)";
System.out.println("Your string:");
System.out.println("str=\"" + str + "\"");
System.out.println();
// remove all ')' since they will not be used
// using space as a delimiter is enough
String str2 = str.replaceAll("\\)", "");
System.out.println("Your string after removing ')' character:");
System.out.println("str2=\"" + str2 + "\"");
System.out.println();
// Since your input has spaces, we split on spaces
String[] strings = str2.split("\\s+");
System.out.println("Result after splitting str2 by spaces:");
for (String s : strings) {
System.out.println(s);
}
System.out.println();
// Lets make two array
String[] num = new String[500];
String[] mark= new String[500];
// loop strings
int cnt = 0;
for (String s : strings) {
String[] a = s.split("\\("); // a[0]="012", a[1]="207.5"
num[cnt] = a[0];
mark[cnt] = a[1];
cnt++;
}
System.out.println("Result num: ");
System.out.print("num[500] = ");
for(String s : num) {
if(s==null) {break;}
System.out.print(s + ",");
}
System.out.println(" etc.\n");
System.out.println("Result mark: ");
System.out.print("mark[500] = ");
for(String s : mark) {
if(s==null) {break;}
System.out.print(s + ",");
}
System.out.println(" etc.\n");
}
}
Output:
Your string:
str="12132002(177) 012(207.5) 014(184)"
Your string after removing ')' character:
str2="12132002(177 012(207.5 014(184"
Result after splitting str2 by spaces:
12132002(177
012(207.5
014(184
Result num:
num[500] = 12132002,012,014, etc.
Result mark:
mark[500] = 177,207.5,184, etc.
Im have to write a method to check if a word is a palindrome. There is probably a easier way then I have it but this is just based off what I have learned so far. My method works except if there is a capital letters compared to a lowercase.
Edit: wasn't very clear. My method returns that a capital and lower case letter are the same. But I would like it to say they are different
public static void printPalindrome(Scanner kb) {
System.out.print("Type one or more words: ");
String s = kb.nextLine();
int count = 0;
for(int i = 0; i < s.length();i++) {
char a = s.charAt(i);
char b = s.charAt(s.length()-(i+1));
if (a==b) {
count ++;
} else {
count = count;
}
}
if (count == s.length()) {
System.out.print(s + " is a palindrome!");
} else {
System.out.print(s + " is not a palindrome.");
}
}
I'd recommend a slightly different approach, I'd reverse the string using StringBuilder#reverse and then compare the two strings using String#equalsIgnoreCase
String s = kb.nextLine();
StringBuilder sb = new StringBuilder(s).reverse();
if (s.equalsIgnoreCase(sb.toString())) {
...
} else {
...
}
You can solve your problem by converting the input String to upper case :
String s = kb.nextLine().toUpperCase();
Or if you wish to preserve the case of the original String, create a new String and test if it's a palindrome.
String s = kb.nextLine();
String u = s.toUpperCase();
int count = 0;
for(int i = 0; i < u.length();i++) {
char a = u.charAt(i);
char b = u.charAt(u.length()-(i+1));
if (a==b) {
count ++;
} else {
count = count;
}
}
i think you can do it with its ascii values
look this picture
then you shoul convert your char to ascii
char character = 'a';
int ascii = (int) character;
then compare the integers