拆分字符串,搜索和翻译子字符串JAVA

时间:2016-05-03 22:16:39

标签: java string substring

我对Java很新,我需要为一项任务解决这个问题。有人可以帮帮我吗?

问题是我们需要输入一个像“AUGUUUUCU”这样的字符串,然后将它分成三个字母的字符串,如“AUG”,“UUU”,“UCU”。

之后我将不得不迭代这些并将它们翻译成“AUG = METIONINA”,“UUU = FENILANINA”,“UCU = SERINA”。谁能帮我这个?

我已经找到了分割它们的方法:

public class Main {

    public static void main(String[] args) {            
        String str = "AUG-UUU-UCU";
        String delimiter = "-";
        String[] temp;
        temp = str.split(delimiter);
        for(int i =0; i < temp.length ; i++)
            System.out.println(temp[i]);
    }    
}

3 个答案:

答案 0 :(得分:1)

如果你应该接受用户输入,那么你需要使用扫描仪:

Scanner sc = new Scanner(System.in);
String input = sc.next();

要将其拆分为三个字母的字符串,请使用数组并使用for循环将子字符串存储在数组中:

String[] subs = new String[input.length()/3];
int index;
for (int i=0; i<input.length(); i++) {
    index = i*3;
    subs[i] = input.substring(index, index+3);
}

然后,您可以使用另一个for循环遍历数组并使用switch语句来确定正确的输出:

for (int i=0; i<subs.length; i++) {
    switch(subs[i]) {
        case "AUG":
            System.out.println("METIONINA");
            break;
        case "UUU":
            System.out.println("FENILANINA");
            break;
        case "UCU":
            System.out.println("SERINA");
            break;
        default:
            break;
    }
}

请注意break块中的switch语句。这些都很重要;如果没有break语句,它将在匹配的大小写后执行所有代码。

答案 1 :(得分:0)

您可以创建一个包含要翻译的值的数组。

translateArray = new String[3];

然后,您可以根据收到的输入设置并行数组的值。然后你可以发布这些值。

for (int i=0; i<temp.length ; i++) {
     if (temp[i] == "AUG" ) {
         translateArray[i] = "METIONINA";
     }
     if (temp[i] == "UUU") {
         translateArray[i] = "FENILANINA";
     }
     if (temp[i] == "UCU") {
         translateArray[i] = "SERINA";
     }
     System.out.println(temp[i] + " = " + translateArray[i]);
}

也许这对你来说会更好。

答案 2 :(得分:0)

要将字符串拆分为字符串,3个字符长为1行:

class AddUpdateProfile(webapp2.RequestHandler):
    def post(self):
        #This will be used to add/update profile in a datastore. Will be called when the user clicks on submit button on the Profile Page
        template = JINJA_ENVIRONMENT.get_template('profile.html')
        error = None
        name = self.request.get('name')
        designation = self.request.get('designation')
        salary = self.request.get('salary')
        currency = self.request.get('currency')
        logging.info("Name = "+name)
        logging.info("Designation = "+designation)
        logging.info("Salary = "+salary)
        logging.info("Currency = "+currency)

        profile = UserProfile(parent=userProfile_key(users.get_current_user().email()))
        profile.name = name
        profile.designation = designation
        profile.salary = int(salary)
        profile.currency = currency
        profile.email = str(users.get_current_user().email())
        profile.put()

        #Go back to main page. TODO : Change this to update 
        self.redirect('/profile')

class Profile(webapp2.RequestHandler):

    def get(self):
        logging.info("Inside Profile Page")
        user = users.get_current_user()

        if user:
            profileInfo = getProfileInformation(user.email())
            logging.info("Found a user inside Profile Page")
            url = users.create_logout_url(self.request.uri)
            if profileInfo is None or not profileInfo:
                logging.info("Email = "+user.email())
                logging.info("Profile Info not found")
                template_values = {
                'user': user.nickname(),
                'url': url
                }
            else:
                logging.info("Profile Info found")
                template_values = {
                'user': user.nickname(),
                'url': url,
                'name' : profileInfo[0].name,
                'designation' : profileInfo[0].designation,
                'salary' : profileInfo[0].salary,
                'currency' : profileInfo[0].currency
                }


            template_values = template_values
            template = JINJA_ENVIRONMENT.get_template('profile.html')
            self.response.write(template.render(template_values))
        else:
            logging.info("User not found. Loading Landing page")
            template_values = {
                'url' : users.create_login_url(self.request.uri)
            }
            template = JINJA_ENVIRONMENT.get_template('landing.html')
            self.response.write(template.render(template_values))


class MainPage(webapp2.RequestHandler):

    def get(self):
        logging.info("Inside MainPage")
        user = users.get_current_user()
        if user:
            logging.info("Found a user inside MainPage")
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'SIGN OUT'
            template_values = {
            'user': user.nickname(),
            'url': url,
            'userPage' : "no",
            'url_linktext': url_linktext,
            }
            template = JINJA_ENVIRONMENT.get_template('index.html')
            self.response.write(template.render(template_values))
        else:
            logging.info("User not found. Loading Landing page")
            template_values = {
                'url' : users.create_login_url(self.request.uri)
            }
            template = JINJA_ENVIRONMENT.get_template('landing.html')
            self.response.write(template.render(template_values))

app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/profile', Profile),
    ('/addProfile', AddUpdateProfile)
], debug=True)