Home > Blockchain >  How to take a string and read it for an array
How to take a string and read it for an array

Time:09-06

Im having trouble finding out how to read letters and turn them into numbers like -1 and 1. Here's the context:

I'm working on a word problem for my Java programming class. I'm asked to create a program in Java that receives input of a number of "L"s or "R"s, short for left or right. For each L the program should go one spot back in an array, and for each R it should go one spot forward. Like if you start at 0, and get an input of RR, it should move to be at 2. Hopefully that makes sense, here's a diagram to hopefully clarify.Example array

Now, what I don't understand is how to take the input from using Scanner(System.in) that gives me the L/R combination (eg. LLR) and turn that into the series of directions for the program (eg. -1,-1, 1). How do i specify that the input of an L is equal to going one space back? And vice versa for any R's input into the program?

Any tips would be greatly appreciated, thanks a ton

Edit: Heres the current code I have:

import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;

    public class Solution {
        public static void main(String args[] ) throws Exception {
            int max = 100;
            int min = 1;
            int posMax = 10000;
            int posMin = -10000;
            boolean dataExists = false;
            
            String inputData;
            int initialPosition;
            System.out.println("Insert sequence of commands (L and R)");
            Scanner input = new Scanner(System.in);
            inputData = input.nextLine();
            System.out.println("Input starting position");
            initialPosition = input.nextInt();
            
        }
    }

What it does it it defines the minimum and maximum commands (left and rights, which is 1-100) and the min and max positions which are -10000 and 10000. The next part would have been recieving the string of L's and R's and reading them to change the array but thats where im stuck.

CodePudding user response:

The return value of Scanner.nextLine() is a String Object. You can split up this String Object to a char array and check if the char is a 'L', 'R', or something else.

String cmds = "RRRRRLLLRLRLLRLRLLRLLRLRL";
char[] cCmds = cmds.toCharArray();
int pos = 39;
for (char c : cCmds) {
    switch (c) {
        case 'L' -> pos--;
        case 'R' -> pos  ;
        default -> System.out.println("That was not an L or R...");
    }
}
System.out.println("Position: "   pos);

Next step would now to add some conditions to check if the user input was too long or too short.

if (cmds.length() > MAX_INPUT || cmds.length() < MIN_INPUT) {
    System.out.println("User input was too long or too short");
    return;
}

The last step now is to check if you can move to another position each time if you want to move.

 case 'L' -> { if (pos > POS_MIN) pos--; }
 case 'R' -> { if (pos < POS_MAX) pos  ; }

All in one, it would look like this (with Scanner):

import java.util.Scanner;

public class Solution {

    static final int MIN_INPUT = 1;
    static final int MAX_INPUT = 100;

    static final int POS_MIN = -10000;
    static final int POS_MAX = 10000;

    public static void main(String[] args) {
        System.out.println("Insert sequence of commands (L and R)");
        Scanner input = new Scanner(System.in);
        String cmds = input.nextLine();

        if (cmds.length() > MAX_INPUT || cmds.length() < MIN_INPUT) {
            System.out.println("User input was too long or short");
            return;
        }

        System.out.println("Input starting position");
        int pos = input.nextInt();

        char[] cCmds = cmds.toCharArray();
        for (char c : cCmds) {
            switch (c) {
                case 'L' -> { if (pos > POS_MIN) pos--; }
                case 'R' -> { if (pos < POS_MAX) pos  ; }
                default -> System.out.println("That was not an L or R...");
            }
        }
        System.out.println("Position: "   pos);
    }
}

Keep in mind to catch the exception which the Scanner Object can cause. (e.g. when scanner.nextInt() gets non Integer user input)

And also add a check if the user input for the initial position is in the given range.

CodePudding user response:

Does this help?

const some_input = ["R", "R", "R", "R", "R", "L"];
const sortnumbs = [19, 22, 18, 19, 16, 18, 19, 21, 24];
let R_target = "R";
let L_target = "L";

let counter = 0;
for (movement of some_input) {
  if (movement == R_target) {
        counter  ;
    }
};

for (movement of some_input) {
  if (movement == L_target) {
        counter--;
    }
};

console.log(sortnumbs[counter]); // 3

  • Related