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