понеділок, 22 грудня 2014 р.

Java task: in the string remove the spaces between the first and the second question mark

Java task: in the string remove the spaces between the first and the second question mark




import java.util.Arrays;
import java.util.List;

//http://pas1.ru/taskstring
//Во введенной строке удалить пробелы между первым и вторым вопросительным знаком.
//http://www.w3schools.com/jsref/jsref_split.asp
public class MyJava1 {
 public static void main(String[] args) {

  String input = "1 fish ? fish re d? fish blue fish ?";
  
  List<String> list = Arrays.asList(input.split(""));
  System.out.println(list);
  int k = 0;
  int z = 0;

  for (int i = 0; i < list.size(); i++) {
   if (list.get(i).contains("?")) {
    k = i;
    break;
   }
  }

  for (int i = k + 1; i < list.size(); i++) {
   if (list.get(i).contains("?")) {
    z = i;
    break;
   }
  }
  
  for (int i = k+1; i < z; i++) {
   if (list.get(i).contains(" ")) {
    list.set(i, "");
   }
  }

  StringBuffer result = new StringBuffer();
  for (int i = 0; i < list.size(); i++) {
     result.append( list.get(i) );
  }
  String mynewstring = result.toString();
  
  System.out.println(list);
  System.out.println(input);
  System.out.println(mynewstring);

 }

}

Немає коментарів:

Дописати коментар