Neil Stuart
Neil Stuart

Reputation: 1

Java Strings return multiple values

I am making a simple script for a school project I want it to print amino acid sequences if I type one letter.

package emmas;

import java.util.Scanner;

public class Emma {
    public static void main(String args[]){
         @SuppressWarnings("resource")
         Scanner Input = new Scanner(System.in);
         int end = 0;
         String f = "A,T,C", e = "A,T,G";

         while (end != -1){
             if(Input.hasNext()){
                  String input = Input.next();
                  Input.nextLine();
                  if(input.equalsIgnoreCase("f")){
                     System.out.println(f);
                  } else if(input.equalsIgnoreCase("e")){
                   System.out.println(e);
                  }
                }
            }
      }
}

This is my code, I want to make it so that if I type for example "ffe" it will return ATC,ATC,ATG I dont know how(this has to apply for up to 20 different letters. I only have 2 here as i haven't added them all).

Upvotes: 0

Views: 88

Answers (4)

hasskell
hasskell

Reputation: 441

Not sure if I understood the question, but if you want to output strings according to some input, for example, ffe will output ATC,ATC,ATG and you will have another 20 characters in, then it's nice to have some data structure in you code like Map where you can associate each character with string value. f => ATC e => ATG

public class Emma {

    public static void main(String[] args) {

        Map<Character, String> aminos = new HashMap<>();

        Character f = 'f';
        Character e = 'e';

        aminos.put(f, "ATC");
        aminos.put(e, "ATG");

        String seq = reader();
        for (int i = 0; i < seq.length(); i++) {
            System.out.print(aminos.get(seq.charAt(i)));
        }
    }

    public static String reader(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter char sequence... ");
        String seq = scanner.next();

        return seq;
    }

}

so you can add another 20 or how many you need characters and assign the value to them. You can see that I eliminate the if else block which makes my code more clear and readable.

Upvotes: 0

Shweta Gulati
Shweta Gulati

Reputation: 606

You can go for switch-case. while (Input.hasNext()) {

  String input = Input.next();
  Input.nextLine();
  switch (input) {
    case "f":
      System.out.println(f);
      break;
    case "e":
      System.out.println(e);
      break;
  }

Check if this solves your problem. One more thing. Your input variable should be in camel case as a best coding practice.

Upvotes: 0

Vic Seedoubleyew
Vic Seedoubleyew

Reputation: 10556

You can either continue to do what you have done so far, or simply build a 'Map' object, initialize it with values, and then refer to it to print the corresponding value.

Initialization:

private static final Map<Character,String> map = new HashMap<>();
static {
   map.put('f', "ACT");
   map.put('e', "ATG");
   ...
}

Usage:

System.out.println( map.get(input.charAt(0) ) );

That way you don't need any ifs in your method.

Other notes

  • you are actually not looking to return several values. You are looking to match a given string against a set of different possible values.
  • if you want to enable several characters to appear contiguous, meaning without space between them, you will need to iterate over each character of your input string :

    for( int i=0; i< input.length(); i++){
        System.out.println( map.get( input.charAt(i) ) );
    }
    
  • in addition to the matters mentioned above, you will need to:

    • add commas to the output
    • check for invalid characters

Upvotes: 2

Amer Qarabsa
Amer Qarabsa

Reputation: 6574

I would recommend using map but no need if you only have few Strings, you can do it like this

         while (Input.hasNext()){
             String in  = Input.next();
             for(int i=0;i< in.length();i++) {
                 if(Character.toLowerCase(in.charAt(i)) == 'f')
                     System.out.print(f+",");
                 else if(Character.toLowerCase(in.charAt(i))=='e')
                     System.out.print(e+",");
             }
             System.out.println("");
         }

Upvotes: 0

Related Questions