0

What I'm trying to do

I'm trying to find an easy way to input multiple arguments in one String and pass parts of it on to get computed.

For example, terminal instructions are usually a String of multible instructions sudo foo bar = [sudo][foo][bar]

I don't know how to do this in java, or how it's supposed to be done.

So far I've tried

ArrayList<Integer> threadList = new ArrayList();


    Scanner scanner = new Scanner(System.in);




    public void printOptions(){
        System.out.println("[1]          > New Primefactordevision");
        System.out.println("[Number]     > Check Status of [Number]");
        System.out.println("[x] [Number] > Terminates Primefactordivision of [Number]");
        System.out.println("[x]          > Terminate");

    }

    public void chooseOperation(){
        printOptions();
        String choice = scanner.next();

        switch(choice){
            case "1":{}
            case "Number":{}
            case String.format("x %s",threadList.get(*choice- x somehow*).toString()):{}
            case "x":{}
        }

The program is supposed to terminate a thread, computing primefactors out of an integer, when you type x [integer contained by threadList].

So basically the String has 2 arguments. [x] and [integer].

While it's not a problem to use x plus a defined integer as static parameter, it's a problem to have part of the choice parameter dynamically allocated from somewhere else, being conditional.

How is this supposed to be done?

8
  • 2
    Afraid I'm not sure what you're asking - do you want to determine if a string contains x, a space, and then a number? If so a regex would be the best fit for that. Commented May 18, 2018 at 11:56
  • why not the same? and use split or regex to get the seperate parts Commented May 18, 2018 at 11:56
  • feel free to answer the question Commented May 18, 2018 at 11:57
  • @berry120 if I understand him right, he wants to get an array of Strings from "[sudo][foo][bar]" with elements "sudo", "foo" and "bar" Commented May 18, 2018 at 11:57
  • @BlkPengu I have answered your question. feel free to try it out Commented May 18, 2018 at 11:57

2 Answers 2

1

I'm trying to have an input composed of the letter x and an integer, of which the integer is to be computed further

If you want to determine if a string matches the format "x 123", where 123 is a number that may vary (and then extract that number), a regex with a matcher is your best bet.

String inputStr = "x 123";

Pattern pattern = Pattern.compile("x (\\d+)");
Matcher matcher = pattern.matcher(inputStr);
if(matcher.find()) {
    String num = matcher.group(1);
    System.out.println(num);            
}
Sign up to request clarification or add additional context in comments.

Comments

1

The following program will read a string and an int from the console and print them

import java.util.Scanner;

public class ScannerTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            String s = sc.next();
            int i = sc.nextInt();
            System.out.println(s+"/"+i);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.