Problems with code/cannot find symbol
A total newbie to Java here (although have managed to make three programs succesfully! :(party): )
I need to create a program that asks for a word and if the number of letters is uneven, it should print out first, last and the "median" letter. If not, it should print the first and last letters. This is the code I've written thus far, mostly trying to emulate my other program which dealt with mere numerics and copy-pasting from the lecture powerpoints:
Code:
import java.util.Scanner;
public class demo1_6 {
public static void main(String[] args) {
Scanner lukija = new Scanner (System.in);
System.out.println ("anna sana ");
string a = lukija.nextLine();
int b = a.length ();
if (a % 2 /= 0) {
System.out.println (a.charAt (0) + a.charAt (b / 2) + a.charAt (b));
}
else {
System.out.println (a.charAt (0) + a.charAt (b));
}
lukija.close();
}
}
When trying to build the file, it gives error messages on the lines where string a and int b are about "cannot find symbol". It's also doubtful my program does anywhere near what it's supposed to do even without error messages. I managed to succesfully create a program which requested user to give three numbers and then print out the median one.
A poor statistics sod shouldn't be forced to learn Java. :frusty: :smash:
Thanks for all the help :)-:
Re: Problems with code/cannot find symbol
First problem found: obviously it's String, not string.
Anyway, now it claims the operandi % isn't cool. It's however what the instructions told me to use.
Re: Problems with code/cannot find symbol
Okey, old problems solved, new problems arise. :smash:
Code:
import java.util.Scanner;
public class demo1_6 {
public static void main(String[] args) {
Scanner lukija = new Scanner (System.in);
System.out.println ("anna sana ");
String a = lukija.nextLine();
int b = a.length ();
if (b % 2 == 0) {
System.out.println (a.charAt (0) + a.charAt (b));
}
else {
System.out.println (a.charAt (0) + a.charAt (b / 2) + a.charAt (b));
}
lukija.close();
}
}
Now this file can be built but when ran and given a word, it tells that
Quote:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:658)
at demo1_6.main(demo1_6.java:22)
Now what?
Re: Problems with code/cannot find symbol
Aaaaaand I made it work! :(party):
Code:
import java.util.Scanner;
public class demo1_6 {
public static void main(String[] args) {
Scanner lukija = new Scanner (System.in);
System.out.println ("anna sana ");
String a = lukija.nextLine();
int b = a.length ();
if (b % 2 == 0) {
System.out.print (a.charAt(0));
System.out.print (a.charAt(b-1));
}
else {
System.out.print (a.charAt (0));
System.out.print (a.charAt (b/2));
System.out.print (a.charAt (b-1));
}
lukija.close();
}
}
Programming is tedious but when it works it's fun! Yay! Didn't even need help.
Re: Problems with code/cannot find symbol
Well, for me, the first step should be to indent your code properly, as I can't follow the flow of that.
Code:
public void someMethod() {
for (int i = 0; i < someArrayLength; i++) {
doSomething();
}
if (somethingIsTrue) {
doThis();
} else {
doThat();
}
}
as an example.
As for your error, you are attempting to read the 6th character of a String that has less than 6 characters.