在java的输出中添加连字符

时间:2015-08-11 11:18:29

标签: java hyphen

所以让我说我想做一个字符到数字转换器,并希望在输出中添加连字符。在前3位数字中使用连字符,然后在每4位数后连字符

e.g。预期产出

    comecomecomecomecome

    266-3266-3266-3266-3266-3

我只设法添加第一个连字符,但如何在4位数后添加子序列连字符?

这里我是如何为第一个连字符做的;

if(  count==3) {
       Number += "-";
   }
   i++;

我现在得到输出

 callhome
 225-54663

这是主要代码

  import java.util.Scanner;

 {
  public static void main (String[] args)

 {
   Scanner  console = new Scanner(System.in);


   {
    System.out.println("Enter the  word to be converted : ");
    String Letter = console.nextLine();
    Letter = Letter.replaceAll("\\s", ""); 
    Letter = Letter.toLowerCase();
    String  Number="";


    int count=0;
     int  i=0;

    while( count < Letter.length()) 
      {switch(Letter.charAt(i))
      {case 'A':case 'B':case 'C': case 'a': case 'b': case 'c':
              Number += "2";
              count++;
      break;
  case 'D':case 'E':case 'F': case 'd': case 'e': case 'f':
               Number += "3";
              count++;
      break;
   case 'G':case 'H':case 'I': case 'g': case 'h': case 'i':
              Number += "4";
              count++;
      break;
    case 'J':case 'K':case 'L': case 'j': case 'k': case 'l':

              Number += "5";
             count++;
      break;
    case 'M':case 'N':case 'O': case 'm': case 'n': case 'o':
          Number += "6";
              count++;
      break;
    case 'P':case 'R':case 'S': case 'p': case 'r': case 's':
              Number += "7";
              count++;
      break;
    case 'T':case 'U':case 'V': case 't': case 'u': case 'v':
            Number += "8";
            count++;
      break;
    case 'W':case 'X':case 'Y':case 'Z': case 'w': case 'x': case 'y': case 'z':
         Number += "9";
         count++;
      break;

      }

    if(  count==3) {
       Number += "-";
   }
   i++;

           }
     System.out.println( Number );

    }


    }}

2 个答案:

答案 0 :(得分:2)

您应该使用modulo operator

if((count % 4) == 3) {
    Number += "-";
}

这将把连字符放在位置之后:3,7,1 ......等等。

答案 1 :(得分:0)

尝试以下... 我修改了程序,它正在运行

package com.test;

import java.util.Scanner;
public class TestPrint 
{
public static void main (String[] args)

{
Scanner  console = new Scanner(System.in);


{
  System.out.println("Enter the  word to be converted : ");
  String Letter = console.nextLine();
  Letter = Letter.replaceAll("\\s", ""); 
 Letter = Letter.toLowerCase();
 String  Number="";


 int count=0,prevCount=0;
 int  i=0;

 while( count < Letter.length()) 
 {switch(Letter.charAt(i))
 {case 'A':case 'B':case 'C': case 'a': case 'b': case 'c':
         Number += "2";
         count++;
 break;
case 'D':case 'E':case 'F': case 'd': case 'e': case 'f':
          Number += "3";
         count++;
 break;
 case 'G':case 'H':case 'I': case 'g': case 'h': case 'i':
         Number += "4";
         count++;
 break;
  case 'J':case 'K':case 'L': case 'j': case 'k': case 'l':

         Number += "5";
        count++;
 break;
  case 'M':case 'N':case 'O': case 'm': case 'n': case 'o':
     Number += "6";
         count++;
 break;
 case 'P':case 'R':case 'S': case 'p': case 'r': case 's':
         Number += "7";
         count++;
 break;
 case 'T':case 'U':case 'V': case 't': case 'u': case 'v':
       Number += "8";
       count++;
 break;
 case 'W':case 'X':case 'Y':case 'Z': case 'w': case 'x': case 'y': case 'z':
    Number += "9";
    count++;
 break;

 }

 if(count==3 || (count-prevCount == 4)) {
  Number += "-";
  prevCount = count;
 }
 i++;

      }
    System.out.println( Number );

  }


 }}