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 3 of 3

Thread: Problem with digits/letters

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with digits/letters

    Making a program which is meant to be able to store information about students, their names, phonenumbers, certain grades etc. I'm all done with the whole program except for one small thing. I want the program to understand that if someone writes i.e. 123 in the field where they should write their name, the program should instead say it's an invalid text, it needs to be in letters. I want similar things on all options, like when the person who uses the program should write the grades, i want them to be able to write nothing except for "ig", "g", "vg", or "mvg" (Swedish grades). Started on the grades so far, can't get them to work though, it's no problem to javac the file, but as soon as I try to run it, it doesn't work the way it should (or at least the way I want it too...)

    Anyway, here's the bit that I'm having problems with (sorry for the swedish):
    "Betyg 1" and "Betyg 2" are the ones I have started with so far (which are the grades).
    switch (Val){
    		case '1':
     
    				System.out.println("Namn: ");
    				String namn = Keyboard.readString();
    				//Want something here that makes "rules" for the name,  
                                     meaning you can only use letters here.			
     
     
    				System.out.println("Personnummer: ");
    				String pn = Keyboard.readString();
    				//Want this one to be able to be only digits and always exactly
                                     10.		
     
     
    				System.out.println ("Betyg 1: ");
    				String betygEtt = Keyboard.readString();
    				if (betygEtt != "ig" || betygEtt != "g" || betygEtt != "vg" || 
                                    betygEtt != "mvg"){
    				System.out.println ("Icke giltigt betyg 1.");
    				}
     
     
    				System.out.println ("Betyg 2: ");
    				String betygTvå = Keyboard.readString();
    				if (betygTvå != "ig" || betygTvå != "g" || betygTvå != "vg" || 
                                    betygTvå != "mvg"){
    				System.out.println ("Icke giltigt betyg 2.");
    				}
     
     
    				System.out.println ("Telefonnummer: ");
    				String tfn = Keyboard.readString();
                                    //Want this one to be able to be only digits.

    Would really appreciate if anyone could help me a little bit, at least give some tips, I'm meant to send this to my teacher tonight, really wanna get it done in time :/


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Problem with digits/letters

    You have a few options here.

    You could look into the Character class.

    The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.

    In addition, this class provides several methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.
    A quick example would be:

    		String str = "Java420";
     
            for (int i = 0; i < str.length(); i++) {
                if (Character.isDigit(str.charAt(i))){
                	System.out.println("Im a number");
                }else{
                	System.out.println("Im not a number");
                }
            }

    Or, if you able to use the Scanner class, you could catch the InputMismatchException.

    For example:

    import java.util.InputMismatchException;
    import java.util.Scanner;
     
    public class Validator {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
    	public static void main(String[] args) {
     
    		Scanner sc = new Scanner(System.in);
     
    		System.out.println("Enter a number: ");
    		try {
    			int myNumber = sc.nextInt();
    			System.out.println("You entered " + myNumber);
    		} catch (InputMismatchException e) {
    			System.out.println("You must enter a number!");
    		}
     
    	}
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem with digits/letters

    if (betygEtt != "ig" || betygEtt != "g" || betygEtt != "vg" || betygEtt != "mvg"){
    Do not compare objects with == and !=, use the equals method instead.

Similar Threads

  1. Extracting letters as variables
    By TH1 in forum Java Theory & Questions
    Replies: 3
    Last Post: February 11th, 2011, 07:22 AM
  2. Remove last digits of an IP address.
    By Jonathan_C in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 15th, 2010, 12:55 PM
  3. Replies: 2
    Last Post: February 19th, 2010, 08:10 AM
  4. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM
  5. Replies: 2
    Last Post: February 4th, 2009, 12:24 PM