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

Thread: Can anyone point me in the right direction with my program?

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Can anyone point me in the right direction with my program?

    The assignment is
    Write a Java program that does the following:
    Prompts the user to input exactly nine characters, which constitude a valid lock combination.
    Gets whatever the user enters and stores it into a String variable, using the the Scanner's .nextLine()
    method.
    Determines if the input string is a valid lock combination - under the following rules:
    1. Consists of exactly nine characters.
    2. The character at subscripts 0 and 6 must be 'R' - to indicate turning the dial to the right.
    3. The character at subscript 3 must be 'L' - to indicate turning the dial to the left.
    4. All other character must be one of the digits: '0' through '9' - indicating what number to
    turn the dial to.
    Example(s) :
    R16L08R19 is valid
    r16L08R19 is not valid
    R16L8R19 is not valid


    So far I have
    import java.util.Scanner;
     
    public class Program4 {
    	public static void main(String[] args) {
    		Scanner stdIn = new Scanner(System.in);
     
    		String response;
    		char R, L;
    		boolean Right, Left;
    		System.out.print("Please enter a lock combination: ");
    		response = stdIn.next();
     
    }}
    Honestly I don't even know where to start. I did a program earlier that was similar, but this one just got super difficult real fast. Can anyone help me. I don't want the answer though, just a starting place.
    Last edited by jeffnine; October 6th, 2014 at 03:56 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Can anyone point me in the right direction with my program? (beginner)

    The assignment is
    Write a Java program that does the following:
    Prompts the user to input exactly nine characters, which constitude a valid lock combination.
    Gets whatever the user enters and stores it into a String variable, using the the Scanner's .nextLine()
    method.
    Determines if the input string is a valid lock combination - under the following rules:
    1. Consists of exactly nine characters.
    2. The character at subscripts 0 and 6 must be 'R' - to indicate turning the dial to the right.
    3. The character at subscript 3 must be 'L' - to indicate turning the dial to the left.
    4. All other character must be one of the digits: '0' through '9' - indicating what number to
    turn the dial to.
    Example(s) :
    R16L08R19 is valid
    r16L08R19 is not valid
    R16L8R19 is not valid


    So far I have
    import java.util.Scanner;
     
    public class Program4 {
    	public static void main(String[] args) {
    		Scanner stdIn = new Scanner(System.in);
     
    		String response;
    		char R, L;
    		boolean Right, Left;
    		System.out.print("Please enter a lock combination: ");
    		response = stdIn.next();
    		R = response.charAt(0);
    		if (R == 'R')
    			Right = true;
    		else Right = false;
     
    		if (Right)
    			System.out.print("R is a valid lock combination!");
     
    		else
    			System.out.print("r is not a valid lock combination!");
     
    }}

    I got a little bit more code in, I'm confused on how to get integers into the code, and I'm also confused on how to get it so that if it's not a valid lock combo, that it would say that that letter is not a valid combo.
    I did a program earlier that was similar, but this one just got super difficult real fast. Can anyone help me. I don't want the answer though, just a starting place.
    Last edited by jeffnine; October 6th, 2014 at 03:57 PM. Reason: Added new code

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    First of you need ask the user for an input and assign that to a variable.

    Now you need loop through the inputted string using the charAt method and check each character within the string to determine if it meets the requirements.

    That's a very good little program to get you thinking. There's isn't that many lines of code.

    Try the above and code it up, then post what you've got.

  4. The Following User Says Thank You to CDeighan For This Useful Post:

    jeffnine (October 6th, 2014)

  5. #4
    Junior Member
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone point me in the right direction with my program? (beginner)

    We aren't using loops yet. We're using if else statements.

  6. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    char first = response.charAt(0);
    Repeat then for the rest of the requirements.

    If(first == 'R')
    {
    If(nextChar == 'R')
    {
    }
    }

    If your not using loops you'll have to use ifs, maybe nested ifs, if your allowed to.

    I'm sorry I'm typing on my phone or I'd rattle something better up.

  7. The Following User Says Thank You to CDeighan For This Useful Post:

    jeffnine (October 6th, 2014)

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Can anyone point me in the right direction with my program?

    @jeffnine: Please post your code correctly using code or highlight tags which are explained near the top of this link.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    jeffnine (October 6th, 2014)

  10. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone point me in the right direction with my program?

    import java.util.Scanner;
     
    public class Program4 {
    	public static void main(String[] args) {
    		Scanner stdIn = new Scanner(System.in);
     
    		String response;
    		char R, L;
    		boolean Right, Left;
     
    		System.out.print("Please enter a lock combination: ");
    		response = stdIn.nextLine();
     
    		char first = response.charAt(0);
    		if (first == 'R')
    			Right = true;
    		else Right = false;
     
    		int second = response.charAt(1);
    		if (second == 'd')
    			Right = true;
    		else Right = false;
     
    		int third = response.charAt(2);
    		if (third == 'd')
    			Right = true;
    		else Right = false;
     
    		char fourth = response.charAt(3);
    		if (fourth == 'L')
    			Left = true;
    		else Left = false;
     
    		int fifth = response.charAt(4);
    		if (fifth == 'd')
    			Right = true;
    		else Right = false;
     
    		int sixth = response.charAt(5);
    		if (sixth == 'd')
    			Right = true;
    		else Right = false;
     
    		char seventh = response.charAt(6);
    		if (seventh == 'R')
    			Left = true;
    		else Left = false;
     
    		int eighth = response.charAt(7);
    		if (eighth == 'd')
    			Right = true;
    		else Right = false;
     
    		int ninth = response.charAt(8);
    		if (ninth == 'd')
    			Right = true;
    		else Right = false;
     
     
    		if (Right)
    			System.out.print( " is a valid lock combination!");
     
    		else
    			System.out.print( " is not a valid lock combination!");
    }}

    How do I make it so that the 'd' is a digit between 0-9? Also it's not making the 'R' and the 'L' a unique letter so that only those letters would work in place 0,3,6. Basically anything on the keyboard could be substituted into the 0,3,6 spots and it would work.

  11. #8
    Junior Member
    Join Date
    Jul 2013
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by jeffnine View Post
    import java.util.Scanner;
     
    public class Program4 {
    public static void main(String[] args) {
    Scanner stdIn = new Scanner(System.in);
     
    String response;
    char R, L;
    boolean Right, Left;
     
    System.out.print("Please enter a lock combination: ");
    response = stdIn.nextLine();
     
    char first = response.charAt(0);
    if (first == 'R')
    Right = true;
    else Right = false;
     
    int second = response.charAt(1);
    if (second == 'd')
    Right = true;
    else Right = false;
     
    int third = response.charAt(2);
    if (third == 'd')
    Right = true;
    else Right = false;
     
    char fourth = response.charAt(3);
    if (fourth == 'L')
    Left = true;
    else Left = false;
     
    int fifth = response.charAt(4);
    if (fifth == 'd')
    Right = true;
    else Right = false;
     
    int sixth = response.charAt(5);
    if (sixth == 'd')
    Right = true;
    else Right = false;
     
    char seventh = response.charAt(6);
    if (seventh == 'R')
    Left = true;
    else Left = false;
     
    int eighth = response.charAt(7);
    if (eighth == 'd')
    Right = true;
    else Right = false;
     
    int ninth = response.charAt(8);
    if (ninth == 'd')
    Right = true;
    else Right = false;
     
     
    if (Right)
    System.out.print( " is a valid lock combination!");
     
    else
    System.out.print( " is not a valid lock combination!");
    }}

    How do I make it so that the 'd' is a digit between 0-9? Also it's not making the 'R' and the 'L' a unique letter so that only those letters would work in place 0,3,6. Basically anything on the keyboard could be substituted into the 0,3,6 spots and it would work.
    I'll take a look at this tomorrow mate post up some additional help when I get In front of PC.

  12. The Following User Says Thank You to CDeighan For This Useful Post:

    jeffnine (October 6th, 2014)

  13. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Can anyone point me in the right direction with my program?

    I suggest you fallback and regroup. Follow the requirements for a valid combination given by the assignment and use those requirements one at a time to determine the validity of the user's input.

    Also

    - in Java variable and method names begin with lowercase letters, and

    - comment your code.

  14. #10
    Junior Member
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone point me in the right direction with my program?

    import java.util.Scanner;
     
    public class Program4 {
    	public static void main(String[] args) {
    		Scanner stdIn = new Scanner(System.in);
     
    		String response; 
    		char r, l;
    		boolean Right, Left;
     
    		System.out.print("Please enter a lock combination: ");
    		response = stdIn.nextLine();
     
    		new Scanner(System.in);
    		char first = response.charAt(0);
    		if (first == 'R')
    			Right = true;
    		else Right = false;
     
    		new Scanner(System.in);
    		char second = response.charAt(1);
    		if (second == 'L')
    			Left = true;
    		else Left = false;
     
    		new Scanner(System.in);
    		char third = response.charAt(2);
    		if (third == 'R')
    			Right = true;
    		else Right = false;
     
     
    		if (Right)
    			System.out.print(first + ("") + second + ("") + third + " is a valid lock combination!");
     
    		else
    			System.out.print(first + ("") + second + ("") + third + " is not a valid lock combination!");

    This code works as expected, just need to get the digits in.

  15. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Can anyone point me in the right direction with my program?

    There's a lot of unnecessary code in each of your blocks of code:
    new Scanner(System.in);
    char first = response.charAt(0);
    if (first == 'R')
    	Right = true;
    else Right = false;
    Why the new anonymous Scanner object before each block? The original Scanner object, stdIn, could be reused, but one isn't needed, and the anonymous Scanner object created at the start of each block is completely useless.

    What purpose do the variables serve? The code in each block could be reduced to:

    // check if first character is an 'R'
    if ( response.charAt[0] != 'R' )
    {
        validCombo =  false;
    }
    Again, take each requirement of the assignment at a time:

    1. Consists of exactly nine characters.
    Write code that checks the length of the input string. Valid if exactly 9 characters.

    2. The character at subscripts 0 and 6 must be 'R' - to indicate turning the dial to the right.
    Check that the characters at 0 and 6 are 'R' . . .

    and so on. Document your code with the requirements, write the code.

Similar Threads

  1. Homework: Point me in the right direction?
    By mattmattmatt in forum Collections and Generics
    Replies: 12
    Last Post: June 4th, 2014, 09:20 PM
  2. Replies: 1
    Last Post: April 6th, 2014, 03:14 AM
  3. Can Anyone Point me in the right direction?
    By ImyMTD in forum AWT / Java Swing
    Replies: 8
    Last Post: May 4th, 2013, 02:29 AM
  4. Can anyone point me in the right direction dealing with bytes
    By derekxec in forum Java Theory & Questions
    Replies: 5
    Last Post: August 18th, 2012, 01:44 PM
  5. [SOLVED] Someone point me to the right direction..
    By ineedhelp in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 30th, 2011, 10:03 PM