我的字符串名称是循环重复?

时间:2014-11-07 11:40:35

标签: java string ascii

问题:删除PRIME ASCII CHARACTERS打印字符串。

这是打印代码表 - > http://en.wikipedia.org/wiki/ASCII#ASCII_printable_code_chart

显然,问题是,如果ASCII数字是素数,则表示您必须删除该字符。这意味着如果我提供输入..

输入

4
MEHTA
Mehta
HELLO
hello

OUTPUT必须

MEHTA
Mht
HELL
hllo

好的,最后我希望你能理解上面的这个问题。所以,就我试过这个代码而言:

package lesson.practice;

import java.util.*;

public class MainProgram {
    public static void main(String[] args){

        Scanner in=new Scanner(System.in);
        System.out.println("Enter your number:");
        int num=in.nextInt();
        String name = null;

        System.out.println("Enter name");
        for(int i=0; i<=num; i++){

            name=in.nextLine();
        }

        String str1=name.replace(new String(Character.toChars(97)), "").
                replace(new String(Character.toChars(101)), "").
                replace(new String(Character.toChars(103)), "").
                replace(new String(Character.toChars(107)), "").
                replace(new String(Character.toChars(107)), "").
                replace(new String(Character.toChars(113)), "").
                replace(new String(Character.toChars(117)), "").
                replace(new String(Character.toChars(119)), "").
                replace(new String(Character.toChars(67)), "").
                replace(new String(Character.toChars(71)), "").
                replace(new String(Character.toChars(73)), "").
                replace(new String(Character.toChars(79)), "").
                replace(new String(Character.toChars(81)), "").
                replace(new String(Character.toChars(83)), "").
                replace(new String(Character.toChars(87)), "").
                replace(new String(Character.toChars(89)), "");

        System.out.println("PRIME ASCII ARE REMOVED:");
        for(int i=1; i<=num; i++){
            System.out.println(str1);
        }
    }
}

但是当我运行我的程序时,它重复了字符串..看看输出

输出

Enter your number:
4
Enter name
Mehta
MEHTA
HELLO
Hello
PRIME ASCII ARE REMOVED:
Hllo
Hllo
Hllo
Hllo

3 个答案:

答案 0 :(得分:0)

您的代码应如下所示:

public static void main(String[] args){

    Scanner in = new Scanner(System.in);
    System.out.println("Enter your number:");
    int num = in.nextInt();
    in.nextLine();

    System.out.println("Enter name");
    String[] result = new String[num];
    for (int i = 0; i < num; i++) {
        String name = in.nextLine();
        String str1 = name.replace(new String(Character.toChars(97)), "")
                .replace(new String(Character.toChars(101)), "")
                .replace(new String(Character.toChars(103)), "")
                .replace(new String(Character.toChars(107)), "")
                .replace(new String(Character.toChars(107)), "")
                .replace(new String(Character.toChars(113)), "")
                .replace(new String(Character.toChars(117)), "")
                .replace(new String(Character.toChars(119)), "")
                .replace(new String(Character.toChars(67)), "")
                .replace(new String(Character.toChars(71)), "")
                .replace(new String(Character.toChars(73)), "")
                .replace(new String(Character.toChars(79)), "")
                .replace(new String(Character.toChars(81)), "")
                .replace(new String(Character.toChars(83)), "")
                .replace(new String(Character.toChars(87)), "")
                .replace(new String(Character.toChars(89)), "");

        result[i] = str1;

    }

    System.out.println("PRIME ASCII ARE REMOVED:");
    for(int i=0; i<num; i++){
        System.out.println(result[i]);
    }

    in.close();
}

答案 1 :(得分:0)

因为您在循环中覆盖了namestr1变量,所以输出是最后一次输入的结果。要保存所有输入然后转换它们,您必须执行以下操作:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter your number:");
    int num = in.nextInt();
    in.nextLine(); // so we get rid of the enter key after the number was pressed

    System.out.println("Enter name");
    String[] names = new String[num];
    for (int i = 0; i < num; i++) {
        String name = in.nextLine();
        String str1 = name.replace(new String(Character.toChars(97)), "").replace(new String(Character.toChars(101)), "")
                .replace(new String(Character.toChars(103)), "").replace(new String(Character.toChars(107)), "")
                .replace(new String(Character.toChars(107)), "").replace(new String(Character.toChars(113)), "")
                .replace(new String(Character.toChars(117)), "").replace(new String(Character.toChars(119)), "")
                .replace(new String(Character.toChars(67)), "").replace(new String(Character.toChars(71)), "")
                .replace(new String(Character.toChars(73)), "").replace(new String(Character.toChars(79)), "")
                .replace(new String(Character.toChars(81)), "").replace(new String(Character.toChars(83)), "")
                .replace(new String(Character.toChars(87)), "").replace(new String(Character.toChars(89)), "");

        names[i] = str1;

    }

    System.out.println("PRIME ASCII ARE REMOVED:");
    for (String removedName : names) {
        System.out.println(removedName);
    }
}

此方法将获取名称输入,删除主要字符并将结果保存在names数组中。然后我们将数组打印到stdout。

答案 2 :(得分:0)

我想我的问题已解决。这就是我编码的内容。

import java.util.*;
import java.io.*;
class ASC{
public static boolean chkprim(int no){
    for(int i=2;i<no;i++){
        if(no%i==0)
        return false;
    }
    return true;
}
public static void main(String args[])throws IOException{
    int n;
    Scanner sc=new Scanner(System.in);

    System.out.println("Enter no. of values");
    n=sc.nextInt();
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int i=1;
    String arr[]=new String[n];
    while(i<=n){
        System.out.println("Enter the word:-");
        String st=br.readLine();
        arr[i-1]=st;
        i++;
    }

    for(i=0;i<n;i++){
        String str=arr[i];
        for(int j=0;j<str.length();j++){
            int num=(int)str.charAt(j);
            if(chkprim(num)==false)
            System.out.print(str.charAt(j));
        }
        System.out.println();
    }
}
}