Question:
Alphabetical order in words error.?
Piers C
2010-10-26 14:24:53 UTC
The program is meant to check if the letters of a word are in alphabetical order. The error is the return and it has to be boolean.
_______________________
package mypkg;

/**
*
* @author piers
*/
public class Main {

public static String isAbecedarian (String word){
int index = 0;
char x = word.charAt(index);
char y = word.charAt(index++);
while (x index = index+1;
boolean truth = true;
}
if (x>y){
boolean truth = false;
}
return (truth);//cannot fix this error.
}
public static void main(String[] args) {
System.out.println (isAbecedarian("abcdef"));//should be true
System.out.println (isAbecedarian("abcaef"));//should be false
}

}
___________________________________________________
Five answers:
The Phlebob
2010-10-26 18:23:19 UTC
These two lines:



char x = word.charAt(index);

char y = word.charAt(index++);



don't set up a RULE to be followed. They initialize x and y one time to the expressions on the right of the = sign.



Code in Java programs is executed in-line, that is, a line of code is executed only when the program counter (excuse an old term) gets to it, not by some external trigger.



Move the two lines inside the while loop. Also, use ++index instead of index++. The first increments index BEFORE it's used; the second AFTER it's used. Since you want to pick up the character after index, you want it incremented before the character is accessed.



Hope that helps.
lavada
2016-06-04 07:31:45 UTC
To follow up on Gary P's answer, the Sholes QWERTY layout is specifically designed to be inefficient and slow the typist down; this would minimize key jamming. In a competition in the 1890's between a number of keyboard layouts, the competitor using the QWERTY layout did something none of the others did; he memorized the keys the way we do today in touch-typing classes. As he won the speed competition, everyone adopted that layout, thinking it was the best. In fact, the Dvorak layout (named for a New Jersey professor who's a relative of the composer) is more efficient, and I understand the US Navy uses it. Despite the inefficiency, in the first 30 days, of people having to unlearn the QWERTY system to learn the Dvorak system, the Navy still finds it worthwhile to use the designed-for-efficiency Dvorak system.
Sean
2010-10-26 14:30:51 UTC
you need to put your check and you chars in your loop, also if it is false you should just return false



aka



loop while index is less to length minus 1 {

set values

if second value is greater than first {

return false

}

}



return true
2010-10-26 14:50:53 UTC
boolean truth = true;

while (x
index = index+1;

}



should work.
Phil
2010-10-26 14:42:43 UTC
your return type for your method requires a string not a boolean.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...