Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem with Nextchar() and scanner...

Options
  • 18-09-2008 11:15am
    #1
    Registered Users Posts: 23,568 ✭✭✭✭


    Hey, first Datastructure lecture back today and Im already haveing trouble with some seriously easy stuff. ominous...

    Question is:

    1)Write a program, called Validate.java that validates user input to ensure that the user enters a lowercase character in the range ‘a’ to ‘z’ inclusive.


    Heres my code so far:
    import java.util.Scanner;


    public class Validate2 {

    /**
    * @param args
    */
    public static void main(String[] args) {


    Scanner scanner = new Scanner(System.in);
    char inputValue;
    do {
    System.out.println( "Enter a Letter in lower case:" );
    inputValue = scanner.nextChar(); ERROR1


    } while (inputValue >=z); ERROR2
    System.out.println( "Input Valid!");

    }

    }


    Basically im having trouble with the 'char' part of the Question.

    at ERROR1 the error is:
    Type mismatch: cannot convert from String to char

    But how am I supposed to use the char if it wont convert. This part really has me fazed...

    at ERROR2 its saying that 'z cannot be resolved'


    Im not to worried about the lower case part for now, just trying to get it recognising letters betwwen a and z... :o


Comments

  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    Im pretty sure scanner.NextChar() will only advance the cursor rather than read the actual char.

    i think you need something like:

    char z;
    do{
    z = scanner.ReadNext(); //or equivalent method i dont know off the top of my head
    }while(scanner.NextChar())


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Ahh college is back time for all the home work posts.


  • Registered Users Posts: 23,568 ✭✭✭✭Frisbee


    kayos wrote: »
    Ahh college is back time for all the home work posts.

    Is one of the functions of this forum not to help people who have a question about programming?

    Of course there'll be more questions when the colleges go back as there will be more people stuck... :confused:


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Yup its here to help people but to be honest home work questions can be a bit of a pain.

    Just to let you know why you are getting the second error you don’t have a variable declared called z.... There is a world of difference between <= z and <= ‘z’

    See it is those basic mistakes that say, to me any way, that you did not even try figure it out for yourself.


  • Registered Users Posts: 23,568 ✭✭✭✭Frisbee


    kayos wrote: »
    Yup its here to help people but to be honest home work questions can be a bit of a pain.

    Why only homework questions as opposed to any questions?
    kayos wrote: »
    Just to let you know why you are getting the second error you don’t have a variable declared called z.... There is a world of difference between <= z and <= ‘z’

    Cheers :)

    kayos wrote: »
    See it is those basic mistakes that say, to me any way, that you did not even try figure it out for yourself.

    I tried to figure it out, I'm still trying to figure it out as I still cant get it to work, I'm just not good at programming.

    I have another part that im stuck on at the moment where I seem to be getting an error message for some reason I cant see on an 'else' statement. But haven't posted that up because I haven't exhausted every option I can think of to try and solve it...


  • Advertisement
  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    First of all kayos if you have a problem with somebody's post then use the Report Post button.

    Second of all Frisbee is within the forum rules so he is welcome to post his question here. Everybody has to start somewhere.

    If you complain in this thread again I'll ban you from the forum.


Advertisement