无法在java中找到字符串

时间:2015-08-03 09:46:31

标签: java

我试图在字符串数组中找到特定的单词。如果单词匹配意味着我想在该单词旁边添加新数字。这是我的计划。

import java.io.IOException;
public class Pincode {

  public static String udf_pincode(String input) throws IOException 
  {
    System.out.println("Inside the function::"+ input);
    String[] Address = input.split("");

    if(Address.equals("Adambakkam"))
    {
        String pincode = "600028";
        double pin = Double.parseDouble(pincode);
        //String pin = pincode.toString()+"adam";

       // return pin;
    }
    else if(Address.equals("Adyar"))
    {
        String pincode = "600088";
        double pin = Double.parseDouble(pincode);
        //  String pin = pincode.toString();
        // return pin;    
    }
    else
    {
        return input;
    }
  }

  public static void main(String args[]) throws IOException
  {
    System.out.println("Inside the function");
    String res = udf_pincode("Adambakkam");
    System.out.println("Output::" + res);
  }
}

数据看起来像这样..

 chennai
 madurai
 bangalore
 hyderabad

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

在代码中添加这些行

Delhi Mumbai

并将您的输入传递为由BufferedReader in = new BufferedReader(new FileReader("path/of/text")); String str; List<String> addressList = new ArrayList<String>(); while((str = in.readLine()) != null){ addressList.add(str); } if (addressList.contains("Adambakkam")) { //code here }

这样的单个空格分隔

为了读取数据行示例文件,请使用以下行

name

答案 1 :(得分:0)

正如@JamesB提到的,您的代码会喜欢这个

  package com.test.tree;

    import java.io.IOException;

    public class StringTest {

        public static String udf_pincode(String input) throws IOException {
            System.out.println("Inside the function::" + input);
            String[] Address = input.split("");
            for(String address : Address){
            if (address.equals("Adambakkam")) {
                String pincode = "600028";
                double pin = Double.parseDouble(pincode);
                // String pin = pincode.toString()+"adam";

                // return pin;
            } else if (address.equals("Adyar")) {
                String pincode = "600088";
                double pin = Double.parseDouble(pincode);
                // String pin = pincode.toString();
                // return pin;
            } else {
                return input;
            }
            }
            return input;
        }

        public static void main(String args[]) throws IOException {
            System.out.println("Inside the function");
            String res = udf_pincode("Adambakkam");
            System.out.println("Output::" + res);
        }
    }