Convert a number-word (String) to its Integer Value - java

EDIT: right, I forgot to state the problem -- which is the fact that I get 0 as an output.
CONTEXT
My program aims to take a user-inputted number-word (1- 99) and output it as an integer (i.e. thirty-four = 34). I can't figure out where the error in my code is and need help:
Scanner scInput = new Scanner(System.in);
String word = scInput.nextLine(); //number in word-form (i.e. twenty six)
char[] charArray = word.toCharArray();//string to char array for word^
int divider = 0; //position of hyphen/space in charArray
All 2-word numbers are comprised of a tens value & a ones value. Assuming proper syntax [english], the word before the hyphen/space divider is the tens and the word following divider is the ones.
ARRAYS
//word values - components & syntax (1-99)
//ONES
public static final String[] wONES = {"one","two","three","four","five","six","seven","eight","nine"};
//TENS
public static final String[] wTENS = {null,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
//TEENS
public static final String[] wTEENS = {"ten", "eleven", "twelve", "thirteen","fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
I've organized all the word-components into 3 different arrays: ones, tens, and teens.
//equivalent integer-array of above String arrays
//ONES
public static final int[] nONES = {1,2,3,4,5,6,7,8,9};
//TENS
public static final int[] nTENS = {0,20,30,40,50,60,70,80,90};
//TEENS
public static final int[] nTEENS = {10,11,12,13,14,15,16,17,18,19};
I created 3 other arrays that are the same as the above three arrays, except they store the integer values.
CODE
Here I separate the user-inputted String into two sections: the tens and the ones. So if the number was 72: 70 = tens and 2 = ones.
int tensValue = 0; //number's tens value (i.e. 30)
int onesValue = 0; //ones value (i.e. 3)
char[] tensArray = null; //array storing tens section of word (before divider)
for (int u = 0; u < divider; u++){
tensArray[u] = charArray[u];
}
String tens = new String(tensArray); //convert char array to String
char[] onesArray = null; //array storing ones section of word (after divider)
for (int u = divider + 1; u > divider && u < charArray.length; u++){
onesArray[u] = charArray[u];
}
String ones = new String(onesArray);
//searches for matches in String array for tens
for(int u = 0; u < wTENS.length; u++){
if(tens.equals(wTENS[u])){
tensValue = nTENS[u];
total += tensValue;
}
}
//searches for matches in String array for ones
for(int u = 0; u < wONES.length; u++){
if(ones.equals(wONES[u])){
onesValue = nONES[u];
total += onesValue;

In your current code you are doing char[] tensArray = null; which should be something like char[] tensArray = new char[10]; or else you end up with NPE.
It might not be most efficient but here is a simple and better approach to your problem.
Read the line and split it on white space (assuming you are separating your words by a space).
Search each of the tokens you get after split in the above lists and add the corresponding number (same index) to your answer.
Print the answer.
Here is the code snippet:
class Main
{
public static final String[] wONES = {"one","two","three","four","five","six",
"seven","eight","nine"};
public static final String[] wTENS = {"ten","twenty","thirty","forty","fifty","sixty",
"seventy","eighty","ninety"};
public static final String[] wTEENS = {"eleven", "twelve", "thirteen","fourteen",
"fifteen", "sixteen", "seventeen", "eighteen",
"nineteen"};
public static final int[] nONES = {1,2,3,4,5,6,7,8,9};
public static final int[] nTENS = {10,20,30,40,50,60,70,80,90};
public static final int[] nTEENS = {11,12,13,14,15,16,17,18,19};
public static void main (String[] args) throws Exception
{
Scanner scInput = new Scanner(System.in);
String word = scInput.nextLine();
int answer = 0;
/* Assuming you are giving space between words */
for(String s : word.split(" ")) {
/* Scan wONES */
for(int i = 0; i < wONES.length; i++) {
if(wONES[i].equalsIgnoreCase(s)) {
answer += nONES[i];
continue;
}
}
/* Scan wTENS */
for(int i = 0; i < wTENS.length; i++) {
if(wTENS[i].equalsIgnoreCase(s)) {
answer += nTENS[i];
continue;
}
}
/* Scan wTEENS */
for(int i = 0; i < wTEENS.length; i++) {
if(wTEENS[i].equalsIgnoreCase(s)) {
answer += nTEENS[i];
continue;
}
}
}
System.out.println("Result: " + answer);
}
}
Input:
thirty four
Output:
34

You have an interesting approach to this problem. A couple of things to change:
I don't see where you set your divider index.
You seem to be doing a lot of work with character arrays, so I'm guessing you're coming from a different language. Sticking with Strings will work fine.
You don't address the "teens". This looks like a simple oversight.
I've added those fixes while attempting maintain the original approach:
public static void main(String [] args) {
Scanner scInput = new Scanner(System.in);
String word = scInput.nextLine();
int total = 0;
int tensValue = 0; //number's tens value (i.e. 30)
int onesValue = 0; //ones value (i.e. 3)
int divider = word.indexOf('-');
String tens = null;
String ones = null;
if (divider != -1) {
tens = word.substring(0, divider);
ones = word.substring(divider + 1);
} else {
ones = word;
}
//searches for matches in String array for tens
if (tens != null) {
for (int u = 0; u < wTENS.length; u++) {
if (tens.equals(wTENS[u])) {
tensValue = nTENS[u];
total += tensValue;
}
}
}
//searches for matches in String array for ones
for(int u = 0; u < wONES.length; u++) {
if (ones.equals(wONES[u])) {
onesValue = nONES[u];
total += onesValue;
}
}
// if a "teen" override what's in total
for(int u = 0; u < wTEENS.length; u++) {
if (ones.equals(wTEENS[u])) {
total = nTEENS[u];
}
}
System.out.println(total);
}

Related

Speed up the comparison time of two strings

The task is to write the logic of checking the magnitude of the coincidence of the player's attempt with the hidden word.
More formally, let there be a string S — a hidden word and a string Q — a player's attempt.
Both strings have the same length N. For each position 1 ≤ i ≤ N of string Q, we need to calculate the type of match in this position with string S.
If Q[i] = S[i], then at position i the match type should be equal to "correct".
If Q[i]≠S[i], but there is another position 1 ≤ j≤ N such that Q[i] = S[j], then in position i the match type must be equal to "present".
Each letter of the string S can be used in no more than one match of
the type "correct" or "present".
Priority is always given to the "correct" type.
Of all possible use cases in the "present" type, the program selects
the leftmost position in the Q string.
In other positions, the match type must be equal to "absent".
Input format:
The first line contains the string S (1≤ S ≤ 10^6) — the hidden word.
The second line contains the string Q ( Q = S) — the player's attempt.
It is guaranteed that the strings S and Q contain only uppercase Latin letters.
Example:
input:
COVER
CLEAR
output:
correct
absent
present
absent
correct
My program does this very slowly, how can I speed it up?
import java.util.*;
public class Task1 {
private final static String cor = "correct";
private final static String abs = "absent";
private final static String pre = "present";
static String[] stringArr;
static java.util.Map<Integer, Integer> map = new HashMap<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
stringArr = new String[a.length()];
int length1 = a.length();
char[] arr1 = a.toCharArray();
char[] arr2 = b.toCharArray();
for (int i = 0; i < length1; i++) {
if (arr2[i] == arr1[i]) {
stringArr[i] = cor;
map.put(i, i);
}
}
for (int i = 0; i < length1; i++) {
if (arr2[i] != arr1[i]) {
while (stringArr[i] == null) {
boolean finded = false;
for (int j = 0; j < arr1.length; j++) {
if (arr2[i] == arr1[j] && !map.containsKey(j)) {
stringArr[i] = pre;
finded = true;
map.put(j, j);
break;
}
}
if (!finded) stringArr[i] = abs;
}
}
}
for (String s : stringArr) {
System.out.println(s);
}
}
}
You can use an int array (or a HashMap<Char, Integer>) count to store those "presented" characters.
We need two for loops. The first loop will set all "correct" positions (if arr1[i] == arr2[i], then stringArr[i] = cor); and for those unmatched positions (arr1[i] != arr2[i]), we count the number of occurrences for those characters in the hidden word (count[arr1[i] - 'A']++).
Then in the second for loop, we check those unmatched positions with the help of count.
If count[arr2[i] - 'A'] > 0, it means that the hidden word contains the character arr2[i], so we can set stringArr[i] = pre and decrement count[arr2[i] - 'A'] (since each letter can be used once)
Otherwise, there's no matching letter in the hidden word, stringArr[i] should be set to abs.
import java.util.Scanner;
public class Task1 {
private final static String cor = "correct";
private final static String abs = "absent";
private final static String pre = "present";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
String[] stringArr = new String[a.length()];
int length1 = a.length();
char[] arr1 = a.toCharArray();
char[] arr2 = b.toCharArray();
int[] count = new int[26];
for (int i = 0; i < length1; i++) {
if (arr2[i] == arr1[i]) {
stringArr[i] = cor;
}
else {
count[arr1[i] - 'A']++;
}
}
for (int i = 0; i < length1; i++) {
if (arr1[i] != arr2[i]) {
if (count[arr2[i] - 'A'] > 0) {
stringArr[i] = pre;
count[arr2[i] - 'A']--;
}
else {
stringArr[i] = abs;
}
}
}
for (String s : stringArr) {
System.out.println(s);
}
}
}
This can be done using hashmaps and taking advantage of the o(1) insert/delete time.
I'm assuming that each word is exactly the same length. Consequently, to get the answer, we need to check each letter in each word. If they have a length of n, then that means at a minimum it will take o(2n) == o(n) to find the answer.
[1] Then, if you're able to use extra space, I would just use a hashmap as an index, where the key is a character in secret, and the value is the set of indices that letter occurs in secret. So it would look something like this:
//cover
c:<0>
o:<1>
etx...
[2] Then check each letter in guess against the index.
If there is no key for a letter, the put absent
If there is a key, then look in the set of indices to see if there is a matching index. Since I used a hashset, that's another o(1) lookup
So sum total, you walk through secret and guess once each which is o(n) runtime. That, plus o(1) for each index lookup produces o(n) runtime.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class Task1 {
public static void main(String[] args)
{
ArrayList<String> res = solve("COVER", "CLEAR");
System.out.println(res);
}
public static ArrayList<String> solve(String secret, String guess){
ArrayList<String> result = new ArrayList<>();
HashMap<Character, HashSet<Integer>> index = new HashMap<>();
//shortcut
if(secret.equals(guess)){
for(int i = 0; i < secret.length(); i++) result.add("correct");
return result;
}
//make the index of char:<indices> for the chars in "secret"
for(int i = 0; i < secret.length(); i++){
char c = secret.charAt(i);
HashSet<Integer> bucket = index.get(c);
if(bucket == null){
bucket = new HashSet<>();
index.put(c, bucket);
}
bucket.add(i);
}
//check each char in "guess" against the index and tally the result
for(int i = 0; i < guess.length(); i++){
char c = guess.charAt(i);
HashSet<Integer> bucket = index.get(c);
if(bucket == null) result.add("absent");
else if(bucket.contains(i)) result.add("correct");
else result.add("present");
}
return result;
}
}

Replacing String in Java to get all variations

I'm trying to get a printout of all variations of a certain String. For example, we have this input: AB0C0. The 0 in the 3rd and 5th spots should be treated as variables. The variable characters are 1, 2, and 3 to be placed in the spot of 0. This means there would be all possible variations of this input:
AB1C1
AB2C1
AB3C1
AB1C2
AB1C3
AB2C2
AB2C3
AB3C2
AB3C3
This is just an example. A 5-character long string is a place for 1 to 5 variables. The issue I'm facing is, that it should generate all variations no matter how many variables are in the input in no matter in which place they are.
Scanner scanner = new Scanner (System.in);
System.out.println("Enter the key consisting of 5 characters:");
String input = scanner.next();
String strOutput1 = input.replaceFirst("0","1");
String strOutput1A = input.replace("0","1");
String strOutput2 = input.replaceFirst("0","2");
String strOutput3 = input.replaceFirst("0","3");
String strOutput4 = input.replaceFirst("0","4");
String strOutput5 = input.replaceFirst("0","5");
System.out.println(strOutput1.toUpperCase());
System.out.println(strOutput1A.toUpperCase());
System.out.println(strOutput2.toUpperCase());
System.out.println(strOutput3.toUpperCase());
System.out.println(strOutput4.toUpperCase());
System.out.println(strOutput5.toUpperCase());
What about this:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the key consisting of 5 characters:");
String input = scanner.next();
//find positions of '0' in input
List<Integer> varPositions = findVarPositions(input);
//create permutations
List<String> permutations = new ArrayList<>();
permutations.add(input);//AB0C0
for (int position : varPositions) {
permutations = permutateAtPosition(permutations, position);
}
//print permutations
for (String permutation : permutations) {
System.out.println(permutation.toUpperCase());
}
}
private static List<Integer> findVarPositions(String input) {
List<Integer> varPositions = new ArrayList<>();
int lastVarPosition = -1;
while ((lastVarPosition = input.indexOf('0', lastVarPosition + 1)) != -1) {
varPositions.add(lastVarPosition);
}
return varPositions;
}
private static List<String> permutateAtPosition(List<String> partialyPermutated, int position) {
List<String> result = new ArrayList<>();
char[] replacements = {'1', '2', '3', '4', '5'};
for (String item : partialyPermutated) {
for (int i = 0; i < replacements.length; i++) {
String output = replaceCharAt(item, position, replacements[i]);
result.add(output);
}
}
return result;
}
private static String replaceCharAt(String input, int position, char replacement) {
//converting to char array, because there's no method like
//String.replaceAtPosition(position, char)
char[] charArray = input.toCharArray();
charArray[position] = replacement;
return new String(charArray);
}
}
It's not fixed to a number of variables.
The idea is to extract positions of '0' and subsequently call the method permutateAtPosition, which takes a partially permutated list and permutates it by one more level.
For "a0b0c0" and values 1-2 it would be ['a0b0c0'], then ['a1b0c0','a2b0c0'], then ['a1b1c0','a1b2c0','a2b1c0','a2b2c0'], and finally ['a1b1c1','a1b1c2','a1b2c1','a1b2c2','a2b1c1','a2b1c2','a2b2c1''a2b2c2'].
This solution keeps everything in memory, so in the general case (unlimited input string) it would be wiser to go with depth-first instead.
I've got another solution for you.
First step, getting the amount of variables:
int variableCount = 0;
for (int i = 0; i < 5; i++) {
if (input.charAt(i) == '0') {
variableCount++;
}
}
Then calculating the amount of results we are expecting:
int countMax = (int)Math.pow(4,variableCount);
Lastly, count up in base 4. Pad the number with 0's and replace the original input 0's:
for (int i = 0; i < countMax; i++) {
String paddedNumbers = format("%" + variableCount + "s",Integer.toString(i, 4)).replace(" ", "0");
int replacedCount = 0;
char[] outputChars = input.toCharArray();
for (int j = 0; j < 5; j++) {
if (input.charAt(j) == '0') {
outputChars[j] = paddedNumbers.charAt(replacedCount);
replacedCount++;
}
}
System.out.println(outputChars);
}

Maximum repeated String in an array

The problem is
how to get the maximum repeated String in an array using only operations on the arrays in java?
so i got into this question in a test and couldn't figure it out.
lets suppose we have an array of string.
str1[] = { "abbey", "bob", "caley", "caley", "zeeman", "abbey", "bob", "abbey" }
str2[] = { "abbey", "bob", "caley", "caley", "zeeman", "abbey", "bob", "abbey", "caley" }
in str1 abbey was maximum repeated, so abbey should be returned and
in str2 abbey and caley both have same number of repetitions and hence we take maximum alphabet as the winner and is returned(caley here).
c > a
so i tried till
import java.util.*;
public class test {
static String highestRepeated(String[] str) {
int n = str.length, num = 0;
String temp;
String str2[] = new String[n / 2];
for (int k = 0;k < n; k++) { // outer comparision
for (int l = k + 1; l < n; l++) { // inner comparision
if (str[k].equals(str[l])) {
// if matched, increase count
num++;
}
}
// I'm stuck here
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter how many votes");
int n = sc.nextInt();
String[] str = new String[n];
for (int i = 0; i < n; i++) {
Str[i] = sc.nextLine();
}
String res = highestRepeated(str);
System.out.println(res + " is the winner");
}
}
so, how should i take the count of occurrence of each string with and attach it with the string itself.
All this, without using a map and any hashing but just by using arrays?
Here is a (unpolished) solution:
static String highestRepeated(String[] str) {
String[] sorted = Arrays.copyOf(str, str.length);
Arrays.sort(sorted, 0, sorted.length, Comparator.reverseOrder());
String currentString = sorted[0];
String bestString = sorted[0];
int maxCount = 1;
int currentCount = 1;
for (int i = 1 ; i < sorted.length ; i++) {
if (currentString.equals(sorted[i])) {
currentCount++;
} else {
if (maxCount < currentCount) {
maxCount = currentCount;
bestString = currentString;
}
currentString = sorted[i];
currentCount = 1;
}
}
if (currentCount > maxCount) {
return currentString;
}
return bestString;
}
Explanation:
Sort the array from highest to lowest lexicographically. That's what Arrays.sort(sorted, 0, sorted.length, Comparator.reverseOrder()); does. we sort in this order because you want the largest string if there are multiple strings with the same number of repeats.
Now we can just count the strings by looping through the array. We don't need a hash map or anything because we know that there will be no more of a string in the rest of the array when we encounter a different string.
currentString is the string that we are currently counting the number of repeats of, using currentCount. maxCount is the number of occurrence of the most repeated string - bestString - that we have currently counted.
The if statement is pretty self-explanatory: if it is the same string, count it, otherwise see if the previous string we counted (currentCount) appears more times than the current max.
At the end, I check if the last string being counted is more than max. If the last string in the array happens to be the most repeated one, bestString won't be assigned to it because bestString is only assigned when a different string is encountered.
Note that this algorithm does not handle edge cases like empty arrays or only one element arrays. I'm sure you will figure that out yourself.
another version
static String lastMostFrequent(String ... strs) {
if (strs.length == 0) return null;
Arrays.sort(strs);
String str = strs[0];
for (int longest=0, l=1, i=1; i<strs.length; i++) {
if (!strs[i-1].equals(strs[i])) { l=1; continue; }
if (++l < longest) continue;
longest = l;
str = strs[i];
}
return str;
}
change in
if (++l <= longest) continue;
for firstMostFrequent
you can't use == to check if two strings are the same.
try using this instead:
if (str[k].equals(str[l])) {
// if matched, increase count
num++;
}

Trouble trying to find a word in a String array

For the code, it wakes a user input and splits it by witespaces then takes the individual words from the user input and checks to see if the singular word is in the text file( containing parallel arrays with one being a string array and the other an int array). For every time it finds the user inputted word it needs to add one but the problem is that I don't know how to implement either match, or compare or equalsTo to check to see if the word is in the String array.
public class MovieReviewSentimentAnalysis {
static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
// TODO: complete me
//make own arrays to pass by value
//movieReviewComments = the text
String[] movieReviewComments = new String[10000];
//movieReviewScores = numeric values, avoid lit. values
int[] movieReviewScores = new int[10000];
String userComment = "";
// String reviewFile = "";
// reviewFile = args[0];
String whiteComment = "";
MovieReviewReader.readMovieReviews("movie_reviews.txt", movieReviewComments, movieReviewScores); //string, string array, and int array
System.out.println("Please type one line of review and when you are done press either Ctr D or Ctr Z");
userComment = userInput.nextLine();
System.out.println(userComment);
String[] words2 = userComment.split("[\\W]");
double itemCount = 0;
double wordTotal = 0;
double totalSumOfUserCommentWords = 0;
String test = "";
// int itemCount = words.length;
for (int i = 0; i < words2.length; i++)
{
test = words2[i];
itemCount = wordCount(test, movieReviewComments, movieReviewScores);
wordTotal += itemCount;
totalSumOfUserCommentWords = wordTotal / userComment.length();
// System.out.println(totalSumOfUserCommentWords);
}
// System.out.println(reviewFile);
System.out.println("Incomplete assignment");
userInput.close();
}
public static double wordCount(String test, String[] movieReviewComments, int[] movieReviewScores)
{
double storeScore = 0;
double totalSumofReviewScores = 0;
double numOfTimesWordAppears = 0;
for (int i=0; i < (movieReviewComments.length); i++)
{
if (test.equals(movieReviewComments[i])) //////////////////////////////////////////////////////////SOMETHING'S OFF
{
storeScore = movieReviewScores[i];
totalSumofReviewScores += storeScore;
numOfTimesWordAppears++;
System.out.println("Found"); //QUQ when will you appear!?!?
}
else
System.out.println("You dun goofed"); //delete after fixing problem
}
double wordScoreAverage = totalSumofReviewScores / numOfTimesWordAppears;
return wordScoreAverage;
}
It is very simple. You can do it the following way:
if (movieReviewComments[i].toLowerCase().contains(test.toLowerCase())
And if you want to test an equal comparison and not containment, use following instead:
if (test.equalsIgnoreCase(movieReviewComments[i])

Array incrementing and i++ issues

I'm trying to use i++ to store certain parts of an array in a loop. But instead of incrementing by 1, I really need it to increment by 2.
For example:
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JOptionPane;
public class PeerTutoring
{
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
int a = 0;
int b = 1;
String name, degree;
String line;
line = JOptionPane.showInputDialog("Inputs");
String[] userinput = line.split("\\s+");
for(int i = 0; i < userinput.length; i++)
{
name = userinput[a];
degree = userinput[b];
a+=2;
b+=2;
}
}
public static String sort(String name)
{
String tutors = "Tutor List";
ArrayList<String> names = new ArrayList<String> ();
names.add(userinput[0]);
names.add(userinput[2]);
names.add(userinput[4]);
names.add(userinput[6]);
names.add(userinput[8]);
names.add(userinput[10]);
names.add(userinput[12]);
names.add(userinput[14]);
names.add(userinput[16]);
names.add(userinput[18]);
}
}
I want a to start as 0 and b as 1, and then each time I would like it to increase by two. (Since the names are each one is seperated by a space) I think I'm going to have to incorporate something along the lines of userinput[a + 1]. But I just wanted to know if there was a simpler way.
for(i=0, i < linesize; i += 2)
As a side note, make sure to initialize your variables outside the loop if you are planning to use them after, or they will be lost.
int a = 0;
int b = 1;
String name, degree;
for(i=0, i < linesize; i += 2)
{
int linesize = line.size();
String line;
line = JOptionPane.showInputDialog("Please enter tutor name and
their highest earned degree.");
String[] userinput = line.split("\\s+");
name = userinput[a];
degree = userinput[b];
a++;
b++;
}
Your options are:
i++; i++; (Which you can't use in the for loop construct since you need a single statement.)
i += 2;
i = i + 2;
String line;
line = JOptionPane.showInputDialog("Please enter tutor name and
their highest earned degree.");
String[] userinput = line.split("\\s+");
for(int i=0, i < userinput.length; ){
String name = userinput[i++];
String degree = userinput[i++];
...
}

Categories

Resources