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

Thread: confused on loop

  1. #1
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default confused on loop

    Okay, so I've been sitting here trying to figure out a solution to get this to work:
    c. After the call to the getSSN(…) method, insert a while statement that tests whether the variable ssn is not an upper or lowercase letter X.

    d. Insert a statement, as you see above properly aligned, that if a successful social security number is entered, it states, “Verifying you entered:”, then concatenates the ssn entered correctly by the user.

    e. Don’t forget a while statement requires a re-prompt to and additional input from the user to determine if staying in the while statement.

    and all I have so far is this for code but can't figure out to basically get it to re-run the prompt over and over until the user enters "x". In the instruction it says it has to have the while loop AFTER the prompt so I'm confused. Here is my code so far:
    import java.util.Scanner;
     
    public class SSNValidatorApp
    {
    	public static void main(String[] args)
    	{
     
    		Scanner sc = new Scanner(System.in);
    		String ssn = Validator.getSSN(sc, "  Enter a Social Security Number in this\n  format (nnn-nn-nnnn) or an \"X\" to exit:  ");
    		while (!ssn.equalsIgnoreCase("x")){
    		System.out.println("  Verifying you entered:\t\t   " + ssn);
    		return;
    	    }
    	    System.out.println("was an x.");
    	}
    }
    and this:
    import java.util.Scanner;
     
    public class Validator
    {
        public static String getSSN(Scanner sc, String prompt)
        {
            System.out.print(prompt);
            boolean isValidSSN = false;
            String ssn = "";
            while (isValidSSN == false)
            {
                ssn = sc.nextLine(); // read entire line
                if (ssn.length() != 11)
                    System.out.println("Error! Invalid Social Security Number. Try again.");
                else if (isNumeral(ssn.charAt(0))
                      & isNumeral(ssn.charAt(1))
                      & isNumeral(ssn.charAt(2))
                      & ssn.charAt(3) == '-'
                      & isNumeral(ssn.charAt(4))
                      & isNumeral(ssn.charAt(5))
                      & ssn.charAt(6) == '-'
                      & isNumeral(ssn.charAt(7))
                      & isNumeral(ssn.charAt(8))
                      & isNumeral(ssn.charAt(9))
                      & isNumeral(ssn.charAt(10)) )
                    isValidSSN = true;
                else
                    System.out.println("Error! Invalid Social Security Number. Try again.");
     
            }
            return ssn;
        }
     
        private static boolean isNumeral(char c)
        {
            if (c == '0' | c == '1' | c == '2' | c == '3' |
                c == '4' | c == '5' | c == '6' | c == '7' |
                c == '8' | c == '9')
                return true;
            else
                return false;
        }
     
        public static String getString(Scanner sc, String prompt)
        {
            System.out.print(prompt);
            String s = sc.next();        // read the first string on the line
            sc.nextLine();               // discard any other data entered on the line
            return s;
        }
     
        public static String getLine(Scanner sc, String prompt)
        {
            System.out.print(prompt);
            String s = sc.nextLine();        // read the whole line
            return s;
        }
     
        public static int getInt(Scanner sc, String prompt)
        {
            boolean isValid = false;
            int i = 0;
            while (isValid == false)
            {
                System.out.print(prompt);
                if (sc.hasNextInt())
                {
                    i = sc.nextInt();
                    isValid = true;
                }
                else
                {
                    System.out.println("Error! Invalid integer value. Try again.");
                }
                sc.nextLine();  // discard any other data entered on the line
            }
            return i;
        }
     
        public static int getInt(Scanner sc, String prompt,
        int min, int max)
        {
            int i = 0;
            boolean isValid = false;
            while (isValid == false)
            {
                i = getInt(sc, prompt);
                if (i <= min)
                    System.out.println(
                        "Error! Number must be greater than " + min);
                else if (i >= max)
                    System.out.println(
                        "Error! Number must be less than " + max);
                else
                    isValid = true;
            }
            return i;
        }
     
        public static double getDouble(Scanner sc, String prompt)
        {
            boolean isValid = false;
            double d = 0;
            while (isValid == false)
            {
                System.out.print(prompt);
                if (sc.hasNextDouble())
                {
                    d = sc.nextDouble();
                    isValid = true;
                }
                else
                {
                    System.out.println("Error! Invalid decimal value. Try again.");
                }
                sc.nextLine();  // discard any other data entered on the line
            }
            return d;
        }
     
        public static double getDouble(Scanner sc, String prompt,
        double min, double max)
        {
            double d = 0;
            boolean isValid = false;
            while (isValid == false)
            {
                d = getDouble(sc, prompt);
                if (d <= min)
                    System.out.println(
                        "Error! Number must be greater than " + min);
                else if (d >= max)
                    System.out.println(
                        "Error! Number must be less than " + max);
                else
                    isValid = true;
            }
            return d;
        }
    }


  2. #2
    Junior Member
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: confused on loop

    Basically you have to call your getSSN method inside the while loop just after the print statement:
    import java.util.Scanner;
     
    public class SSNValidatorApp
    {
    	public static String ssn;
        public static void main(String[] args)
        {
     
            Scanner sc = new Scanner(System.in);
            ssn = Validator.getSSN(sc, "  Enter a Social Security Number in this\n  format (nnn-nn-nnnn) or an \"X\" to exit:  ");
            while (!ssn.equalsIgnoreCase("x")){
            System.out.println("  Verifying you entered:\t\t   " + ssn);
            ssn = Validator.getSSN(sc, "  Enter a Social Security Number in this\n  format (nnn-nn-nnnn) or an \"X\" to exit:  ");
            }
            System.out.println("was an x.");
        }
    }

    import java.util.Scanner;
     
    public class Validator
    {
        public static String getSSN(Scanner sc, String prompt)
        {
            System.out.print(prompt);
            boolean isValidSSN = false;
            String ssn = "";
            while (isValidSSN == false)
            {
                ssn = sc.nextLine(); // read entire line
                if (ssn.charAt(0) == 'x') {
                	return ssn;
                }
                if (ssn.length() != 11)
                    System.out.println("Error! Invalid Social Security Number. Try again.");
                else if (isNumeral(ssn.charAt(0))
                      & isNumeral(ssn.charAt(1))
                      & isNumeral(ssn.charAt(2))
                      & ssn.charAt(3) == '-'
                      & isNumeral(ssn.charAt(4))
                      & isNumeral(ssn.charAt(5))
                      & ssn.charAt(6) == '-'
                      & isNumeral(ssn.charAt(7))
                      & isNumeral(ssn.charAt(8))
                      & isNumeral(ssn.charAt(9))
                      & isNumeral(ssn.charAt(10)) )
                    isValidSSN = true;
                else
                    System.out.println("Error! Invalid Social Security Number. Try again.");
     
            }
            return ssn;
        }
     
        private static boolean isNumeral(char c)
        {
            if (c == '0' | c == '1' | c == '2' | c == '3' |
                c == '4' | c == '5' | c == '6' | c == '7' |
                c == '8' | c == '9')
                return true;
            else
                return false;
        }
     
        public static String getString(Scanner sc, String prompt)
        {
            System.out.print(prompt);
            String s = sc.next();        // read the first string on the line
            sc.nextLine();               // discard any other data entered on the line
            return s;
        }
     
        public static String getLine(Scanner sc, String prompt)
        {
            System.out.print(prompt);
            String s = sc.nextLine();        // read the whole line
            return s;
        }
     
        public static int getInt(Scanner sc, String prompt)
        {
            boolean isValid = false;
            int i = 0;
            while (isValid == false)
            {
                System.out.print(prompt);
                if (sc.hasNextInt())
                {
                    i = sc.nextInt();
                    isValid = true;
                }
                else
                {
                    System.out.println("Error! Invalid integer value. Try again.");
                }
                sc.nextLine();  // discard any other data entered on the line
            }
            return i;
        }
     
        public static int getInt(Scanner sc, String prompt,
        int min, int max)
        {
            int i = 0;
            boolean isValid = false;
            while (isValid == false)
            {
                i = getInt(sc, prompt);
                if (i <= min)
                    System.out.println(
                        "Error! Number must be greater than " + min);
                else if (i >= max)
                    System.out.println(
                        "Error! Number must be less than " + max);
                else
                    isValid = true;
            }
            return i;
        }
     
        public static double getDouble(Scanner sc, String prompt)
        {
            boolean isValid = false;
            double d = 0;
            while (isValid == false)
            {
                System.out.print(prompt);
                if (sc.hasNextDouble())
                {
                    d = sc.nextDouble();
                    isValid = true;
                }
                else
                {
                    System.out.println("Error! Invalid decimal value. Try again.");
                }
                sc.nextLine();  // discard any other data entered on the line
            }
            return d;
        }
     
        public static double getDouble(Scanner sc, String prompt,
        double min, double max)
        {
            double d = 0;
            boolean isValid = false;
            while (isValid == false)
            {
                d = getDouble(sc, prompt);
                if (d <= min)
                    System.out.println(
                        "Error! Number must be greater than " + min);
                else if (d >= max)
                    System.out.println(
                        "Error! Number must be less than " + max);
                else
                    isValid = true;
            }
            return d;
        }
    }
    Last edited by jesamjasam; December 11th, 2010 at 05:06 PM.

  3. #3
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: confused on loop

    Yeah I attempted that earlier, I actually got it going by just doing a nested While loop (I think it should be good enough)

    import java.util.Scanner;
     
    public class SSNValidatorApp
    {
    	public static void main(String[] args)
    	{
     
    		Scanner sc = new Scanner(System.in);
    		String choice = "y";
    		String ssn = "";
    		while (choice == "y"){
    		ssn = Validator.getSSN(sc, "  Enter a Social Security Number in this\n  format (nnn-nn-nnnn) or an \"X\" to exit:  ");
    		while (!ssn.equalsIgnoreCase("x")){
    		System.out.println("  Verifying you entered:\t\t   " + ssn);
    		break;
    	    }
    	    continue;
    	}
    	    System.out.println("was an x.");
    	}
    }

  4. #4
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: confused on loop

    Ok so I guess I still am having issues(differently then the prior questions) and can't seem to figure it out.

    This is what the error says:
    Validator.java:16: non-static method getArrayList() cannot be referenced from a static context
    getArrayList();
    ^
    now I know that its a problem with a static declaration but when i shift them around (delete/add 'static') nothing seems to work, Im hoping someone can figure it out for me...

    import java.util.*;
     
    public class Validator
    {
    		ArrayList<String> ssns = new ArrayList<String>();
        public static String getSSN(Scanner sc, String prompt)
        {
     
            System.out.print(prompt);
            boolean isValidSSN = false;
            String ssn = "";
            while (isValidSSN == false)
            {
                ssn = sc.nextLine(); // read entire line
                if (ssn.equalsIgnoreCase("x"))
    				getArrayList();
    				terminateApp();
                if (ssn.length() != 11)
                    System.out.print("\n  Error!\n  Invalid Social Security Number. Try again:\t");
     
                else if (isNumeral(ssn.charAt(0))
                      & isNumeral(ssn.charAt(1))
                      & isNumeral(ssn.charAt(2))
                      & ssn.charAt(3) == '-'
                      & isNumeral(ssn.charAt(4))
                      & isNumeral(ssn.charAt(5))
                      & ssn.charAt(6) == '-'
                      & isNumeral(ssn.charAt(7))
                      & isNumeral(ssn.charAt(8))
                      & isNumeral(ssn.charAt(9))
                      & isNumeral(ssn.charAt(10)) )
                    isValidSSN = true;
     
     
                else
                    System.out.println("Error! Invalid Social Security Number. Try again:\t");
            }
     
            return ssn;
        }
     
        private static boolean isNumeral(char c)
        {
            if (c == '0' | c == '1' | c == '2' | c == '3' |
                c == '4' | c == '5' | c == '6' | c == '7' |
                c == '8' | c == '9')
                return true;
            else
                return false;
        }
     
        public static String getString(Scanner sc, String prompt)
        {
            System.out.print(prompt);
            String s = sc.next();        // read the first string on the line
            sc.nextLine();               // discard any other data entered on the line
            return s;
        }
     
        public static String getLine(Scanner sc, String prompt)
        {
            System.out.print(prompt);
            String s = sc.nextLine();        // read the whole line
            return s;
        }
     
        public static int getInt(Scanner sc, String prompt)
        {
            boolean isValid = false;
            int i = 0;
            while (isValid == false)
            {
                System.out.print(prompt);
                if (sc.hasNextInt())
                {
                    i = sc.nextInt();
                    isValid = true;
                }
                else
                {
                    System.out.println("Error! Invalid integer value. Try again.");
                }
                sc.nextLine();  // discard any other data entered on the line
            }
            return i;
        }
     
        public static int getInt(Scanner sc, String prompt,
        int min, int max)
        {
            int i = 0;
            boolean isValid = false;
            while (isValid == false)
            {
                i = getInt(sc, prompt);
                if (i <= min)
                    System.out.println(
                        "Error! Number must be greater than " + min);
                else if (i >= max)
                    System.out.println(
                        "Error! Number must be less than " + max);
                else
                    isValid = true;
            }
            return i;
        }
     
        public static double getDouble(Scanner sc, String prompt)
        {
            boolean isValid = false;
            double d = 0;
            while (isValid == false)
            {
                System.out.print(prompt);
                if (sc.hasNextDouble())
                {
                    d = sc.nextDouble();
                    isValid = true;
                }
                else
                {
                    System.out.println("Error! Invalid decimal value. Try again.");
                }
                sc.nextLine();  // discard any other data entered on the line
            }
            return d;
        }
     
        public static double getDouble(Scanner sc, String prompt,
        double min, double max)
        {
            double d = 0;
            boolean isValid = false;
            while (isValid == false)
            {
                d = getDouble(sc, prompt);
                if (d <= min)
                    System.out.println(
                        "Error! Number must be greater than " + min);
                else if (d >= max)
                    System.out.println(
                        "Error! Number must be less than " + max);
                else
                    isValid = true;
            }
            return d;
        }
        public void getArrayList()
        {
    		System.out.println(ssns);
    	}
        public static void terminateApp()
        {
    		    System.out.printf("\n  Program terminated by the user...");
    		    System.out.printf("\n");
     
    			System.out.printf("\n");
    			System.exit(0);
    		}
    }

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: confused on loop

    Replace..

    public void getArrayList()

    with...

    public static void getArrayList()

  6. #6
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: confused on loop

    Validator.java:150: non-static variable ssns cannot be referenced from a static context
    System.out.println(ssns);
    ^


    Thats why I said I tried a lot of different solutions but nothign is working.

  7. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: confused on loop

    Well to get it to compile.. just change your ArrayList to this:
    static ArrayList<String> ssns = new ArrayList<String>();

    But..
    as you don't have a main() in you're current class, and unless you NEED everything to be static for a certain reason, it would be better if you removed the static keyword all together and simply make another class, and make an instance of Validator.

    -I don't know if that will help as I haven't read your program specification sorry

Similar Threads

  1. Confused with Arrays
    By Solidius in forum Collections and Generics
    Replies: 3
    Last Post: October 29th, 2010, 09:54 AM
  2. Confused about setting up JSP
    By mjpam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: September 17th, 2010, 05:14 AM
  3. Ayudame! :confused:
    By wasaki in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 22nd, 2010, 10:37 AM
  4. Replies: 3
    Last Post: February 26th, 2009, 03:04 AM
  5. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 02:18 PM