Java version issue, or code issues
After a very long time away I'm back trying to learn Java. I've found some good lessons but twice I've come into issues which don't make sense, I'm wondering if you guys can help.
Also, I can code VB/PHP and a few others to a basic level so I know what I'm doing, to an extent.
Basically, here's the Java code I've been given:
Code :
import java.util.*;
public class WhileLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char ans;
System.out.println("Do you want to go into the loop [Y/N] > ");
ans = sc.next()charAt(0);
while(ans == 'Y')
{
System.out.println("Do you want to stay in the loop [Y/N] > ");
ans = sc.next()charAt(0);
}
System.out.println("Program Finished");
}
}
All I have to do is copy that down into Eclipse, run it and explain what happens then modify it. I can explain what happens without running it, but when I do try run it I get these errors:
Code :
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from String to char
Syntax error, insert ";" to complete Statement
The method charAt(int) is undefined for the type WhileLoop
Type mismatch: cannot convert from String to char
Syntax error, insert ";" to complete Statement
The method charAt(int) is undefined for the type WhileLoop
at WhileLoop.main(WhileLoop.java:11)
I've spent some time Googling the issues and have changed my project compiler from 1.6, to 1.7, and also changed my JRE's from jre6, to jre7, and to jdk1.7.0_13 and still get the same issue.
I was also given some code in a prior lesson which would use a case statement with strings, which had me assuming I needed the newest version of Java SE/Java DK and I thought I did. But even when I changed all my project settings and such I still couldn't get it to work, so I left it.
arghhh. I'm so lost. Please help lol :thumbsup:
P.s. as an added note, I just tried to install jdk-7 and during the installation it said I already had a newer version of Java installed..... ugh
Re: Java version issue, or code issues
You've got a bug in your code on the line the compiler complains at, line 11:
Code :
ans = sc.next()charAt(0);
You're missing something there -- look carefully at that line, including every dot, and every word, and you should see it. ;)