mercredi 13 mai 2015

Issue assigning a value for each character of input and display the value for input

I'm trying to create a program that accepts user input, scans each substring and checks if that character matches a position in the alphabet and if it does I want to get the equivalent position in the score String to get the score for that character and display the score for each character and the overall total. What I have in my code below should have completed each part of what I'm trying to do, but there's a problem with the second for loop which I cannot find the solution for, I think it may be with the substrings inside the loops but I'm not sure. Any advice or tips I would really appreciate since I think I have the program mostly finished.

Here's question that I'm studying from http://ift.tt/1AX5xHS

import java.util.*;
import java.io.*;
public class testt
{
    public static void main(String [] args) throws IOException
    {
        String alpha = "abcdefghijklmnopqrstuvwxyz";
        String score = "13321424185131139111144849";
        String result = "";
        int value = 0, total = 0;
        Scanner in = new Scanner(System.in);
        System.out.print("Enter word: ");
        String input = in.nextLine();
        if(input == null || input.length() == 0)
        {
            System.out.println("Enter input.");
            System.exit(0);
        }
        String inputLower = input.replaceAll("[^a-zA-Z ]", "").toLowerCase();
        String inputArray[] = inputLower.split("\\s+");
        for(int i = 0; i < alpha.length(); i++)
            if(inputLower.substring(i, i + 1) == alpha.substring(i, i + 1))
            {
                value = Integer.parseInt(score.substring(i, i + 1));
                if(value == 9)
                    value++;
                result += inputLower.substring(i, i + 1) + " will give you " + value + " point(s)\n";
                total += value;
            }
            else if(i == alpha.length() + 1)
            {
                System.out.println(result + "This word will earn you: " + total);
                System.exit(0);
            }

    }
}

Aucun commentaire:

Enregistrer un commentaire