io - java: converting capital chars to ints -


i working on usaco problem (ride) , trying convert capital char (i.e.'a') it's respective int (for 'a' 1) , not seem working. doing is:

for(char c1 : st1ch) {     int charint = (int)c1;     totalcharsum1 = totalcharsum1*charint; } 

..in order convert read string file (which converted array of chars) int counterparts. assumed , read (int)"a" etc. 1. however, code not produce right result apparently. believe problem can see no other problems. have found no guide problem. of course mistake may elsewhere ill post code below anyway:

import java.io.*;  class ride {      public static void main(string[] arg) throws ioexception{          bufferedreader reader = new bufferedreader(new filereader ("ride.in"));         string st1 = reader.readline();         string st2 = reader.readline();         int totalcharsum1 = 1;         int totalcharsum2 = 1;         char[] st1ch = st1.tochararray();         char[] st2ch = st2.tochararray();          for(char c1 : st1ch)         {             int charint = (int)c1;             totalcharsum1 = totalcharsum1*charint;         }         for(char c2 : st2ch)         {             int charint = (int)c2;             totalcharsum2 = totalcharsum2*charint;         }         printwriter out = new printwriter(new bufferedwriter(new filewriter("ride.out")));         if(totalcharsum1%47 == totalcharsum2%47)         {             out.println("go");         }else{             out.println("stay");         }         out.close();         system.exit(0);     }   } 

my questions how convert capital char respective int on alphabet? thanks, sam.

you can convert char int in java subtracting absolute ascii values. below method returns product of integer values of each character in string (useful ride.java):

public static int makenumber(string name) {         int product = 1;         //iterate through each character in string name         (int = 0; < name.length(); i++) {             //get character value             int charval = (name.charat(i) - 'a') + 1;              //multiply product character value             product *= charval;         }         return product;     } } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -