Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java version issue, or code issues

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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:

    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


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java version issue, or code issues

    You've got a bug in your code on the line the compiler complains at, line 11:
        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.

Similar Threads

  1. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  2. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  3. Java Abstract class and Interface issues in a code
    By John234 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 09:51 PM
  4. Version issue? Or what else can it be?
    By SxN in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 17th, 2011, 05:53 PM
  5. Beginner trying to write Java code, has issue w/ printing result and 2 decimals
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 11:11 AM