I can't find out why my code is returning 0, could someone please help? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I can't find out why my code is returning 0, could someone please help?

package tic.tac.toe; import java.util.Scanner; /** * * @author 25688 */ public class TicTacToe { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Average place of a word in the alphabet, enter a string in all caps and no numbers."); String str; str = keyboard.next(); double convo[]; String Alphabet = "0ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int Sentlength = str.length(); int Counter = 0; convo = new double[Sentlength]; for(char Letter; Counter >= Sentlength; Counter++ ){ Letter = str.charAt(Counter); convo[Counter] = Alphabet.indexOf(Letter); } double temp = 0; for(int counter = 0; counter >= Sentlength; counter++){ temp = temp + convo[counter]; } double Result; Result = temp / Sentlength; System.out.println("Average alphabet placement of sentence is " + Result); } }

29th Apr 2019, 1:04 PM
JOE DAVIS
JOE DAVIS - avatar
4 Answers
+ 3
I think your for loop is wrong. int counter = 0; for(char letter; counter >= sentlength... 0 is not >= sentlength --> counter < sentlength If counter <= sentlength you get an error because of convo[counter] --> index starts at 0 and ends at length - 1
29th Apr 2019, 1:19 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
29th Apr 2019, 1:27 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Thank you, I was having trouble with that for quite a while.
29th Apr 2019, 1:30 PM
JOE DAVIS
JOE DAVIS - avatar
0
It's supposed to return the average place of a word in the alphabet.
29th Apr 2019, 1:11 PM
JOE DAVIS
JOE DAVIS - avatar