Member Avatar for Gsterminator

So my problem is that i'm trying to read a MyString that returns a new Mystring in which every digit -0123456789- has been replaced with a certain char.
Ex:
Blink182 ====>Blinkxxx

import java.io.*;
import java.util.Scanner;
////////////////////////////////////////////////////////////////////////////////
class Hw08
{
//------------------------------------------------------------------------------
   public static void main (String [] args) throws Exception
   {
      Scanner kb = new Scanner(System.in);
		MyString s = MyString.read(System.out,kb);
			MyString s2 = s.replaceDigits('x');
      	System.out.println("You entered the MyString \"" + s + "\".");
      	System.out.println("Results of replaceDigits: " + s2);
   }
//------------------------------------------------------------------------------
} // end class Prog1406
////////////////////////////////////////////////////////////////////////////////
class MyString
{
   private char [] a;
//------------------------------------------------------------------------------
   public MyString ( String s )
   {
      this.a = new char[s.length()];
      for ( int i = 0 ; i < s.length() ; i++ )
      {
         this.a[i] = s.charAt(i);
      }
   }
//------------------------------------------------------------------------------
	public MyString replaceDigits(char c)
	{
		char[] result = new char[a.length];
		for(int i = 0; i <a.length; i++)
			if (a[i]==c) result[i] = c;
			else			 result[i]= a[i];
		return new MyString(result);
	}
//------------------------------------------------------------------------------
   public MyString ( char[] a ) { this.a = a; }
//------------------------------------------------------------------------------
//
// MyString ( char[] a, int used )
//
// Constructs a MyString from an unfilled array of char
//
   public MyString ( char[] a, int used )
   {
      this.a = new char[used];
      for ( int i = 0 ; i < used ; i++ ) this.a[i] = a[i];
   }
//------------------------------------------------------------------------------
   public int length () { return a.length; }
//------------------------------------------------------------------------------
   public static MyString read ( PrintStream ps, Scanner sc )
   {
      if ( ps != null ) ps.print("Enter a String: ");
      String s = sc.nextLine();
      return new MyString(s);
   }
//------------------------------------------------------------------------------
   public String toString ()
   {
      String result = "";
      for ( int i = 0 ; i < a.length ; i++ )
      {
         result += a[i];
      }
      return result;
   }
//------------------------------------------------------------------------------
   public static boolean isDigits(char c)
   {
      return c =='0'||c =='1' || c =='2'|| c =='3'|| c =='4'|| c =='5'||
       c =='6'|| c =='7' ||c == '8'|| c =='9';
   }
//------------------------------------------------------------------------------
} // end class MyString
////////////////////////////////////////////////////////////////////////////////

Recommended Answers

All 3 Replies

What is the problem is there any error or something?
It seems working!!

public MyString replaceDigits(char c)
	{
		char[] result = new char[a.length];
		for(int i = 0; i <a.length; i++)
			if (isDigits(a[i]) result[i] = c;
			else			 result[i]= a[i];
		return new MyString(result);
	}

Use this .. It might help. I went through your code and at no point do you use the isDigits function to check whether a character is a digit or not. Also state your question exactly.? what is the output that you are getting now ?

Member Avatar for Gsterminator

sorry for being unclear but yeah i was trying to figure out how to put isDigits() in the parameter. But anyways this did solve my problem thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.