10 Replies - 3416 Views - Last Post: 09 June 2008 - 03:49 PM Rate Topic: -----

#1 colinwsun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-June 08

Need Help for java homework, "phone number validation"

Posted 07 June 2008 - 12:32 AM

here is my code:
import javax.swing.*;
public class sun_wenqian_A2Q1 {
  public static void main(String args[]){
	
	//declare variables within main
	String strInput;
	String digits;
	
	
	//asking user to enter input
	strInput = JOptionPane.showInputDialog("Please enter a phone number or press q to quit the program!");

	while(strInput.equals("q")){
	  digits = strInput.Replace("(","").Replace(")","").Replace("-","");
	  
	  int i = 0; // declare i for loops 
	  
	  while(Character.isDigit(digits.charAt(i))){
		i++;
		numDigits = Integer.parseInt(digits);
		
		//4 kind of Phone numbers:nnnnnn,nnnnnnnnnn,nnnnnnnnnnn,if it's not "204",it's long distance
		if(numberDigits = 7){
		  Systerm.out.println("User entered: "+strInput);
		  Systerm.out.println("Phone number parsed: ");
		  Systerm.out.println("Valide phone number: ");
		}else if(numberDigits = 10){
		  Systerm.out.println("User entered: "+strInput);
		  Systerm.out.println("Phone number parsed: ");
		  Systerm.out.println("Area code: ");
		  Systerm.out.println("Valide phone number: ");
		}else if(numberDigits = 11){
		  Systerm.out.println("User entered: "+strInput);
		  Systerm.out.println("Phone number parsed: ");
		  Systerm.out.println("Area code: ");
		  Systerm.out.println("Valide phone number: ");
		}else{
		  Systerm.out.println("User entered: "+strInput);
		  Systerm.out.println("No Phone number parsed, invalid!");
		}//end of if else if 
		
		  
		
	  }//end of while loops of "isDigits"
	  Systerm.out.println("User entered: "+strInput);
	  Systerm.out.println("No Phone number parsed, invalid!");
	  
	  
	  
	  //strInput = JOptionPane.showInputDialog("Please enter a phone number or press q to quit the program!");
	}//end of while of "enter q to quit"
	  Systerm.out.println("Please enter a valid phone number!!");
  }//end of main
}



when i compile the code, dr java keep telling me
"File: /Users/wsun/Documents/Course summer/Comp 1010/Assignments/Work it out assgn 02/sun_wenqian_A2Q1.java [line: 23]
Error: cannot find symbol
symbol : method Replace(java.lang.String,java.lang.String)
location: class java.lang.String"

i have tried a few things, but nothing come out.
plz help

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help for java homework, "phone number validation"

#2 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: Need Help for java homework, "phone number validation"

Posted 07 June 2008 - 12:49 AM

Quote

digits = strInput.Replace("(","").Replace(")","").Replace("-","");


Correct me if i'm wrong.. but I don't think this is possible.. or atleast not the proper way to apply the string.replace method..
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Need Help for java homework, "phone number validation"

Posted 07 June 2008 - 01:18 AM

Yea, it takes two parameters, which are both java strings. I don't think you can call it from within itself either.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: Need Help for java homework, "phone number validation"

Posted 07 June 2008 - 05:33 AM

View Postmensahero, on 7 Jun, 2008 - 03:49 AM, said:

Quote

digits = strInput.Replace("(","").Replace(")","").Replace("-","");


Correct me if i'm wrong.. but I don't think this is possible.. or atleast not the proper way to apply the string.replace method..


View PostKYA, on 7 Jun, 2008 - 04:18 AM, said:

Yea, it takes two parameters, which are both java strings. I don't think you can call it from within itself either.


Sorry folks, chaining method results is perfectly fine, as long as you get the syntax right. ;) This works:

Quote

digits = strInput.replace("(","").replace(")","").replace("-","");


It is identical to this:

Quote

String result = strInput.replace("(","");
result = result.replace(")","");
result = result.replace("-","");
digits = result;


The String method "replace" return a new string. So, what you're doing is calling the method of the resulting value, and then calling that result's method, and so on.

Unfortunately, after all that, this would actually be more effective:

Quote

digits = strInput.replaceAll("[^0-9]", "");


The replace functions in Java use regular expressions. You can often think of them as simple find and replace, but the find part can choke if you give it a special character. In this last one, what we're saying is where the character is not (^) in the range 0-9, make it blank.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#5 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: Need Help for java homework, "phone number validation"

Posted 07 June 2008 - 06:36 AM

Was that JAVA code, whatever here is the correct syntax I did not saw the logic.
import javax.swing.*;
public class sun_wenqian_A2Q1{
  public static void main(String args[]){
	
	//declare variables within main
	String strInput;
	String digits;
	
	
	//asking user to enter input
	strInput = JOptionPane.showInputDialog("Please enter a phone number or press q to quit the program!");

	while(strInput.equals("q")){
	  digits = strInput.replace("(","").replace(")","").replace("-","");
	  
	  int i = 0; // declare i for loops
	  int numDigits;
	  while(Character.isDigit(digits.charAt(i))){
		i++;
		numDigits = Integer.parseInt(digits);
		
		//4 kind of Phone numbers:nnnnnn,nnnnnnnnnn,nnnnnnnnnnn,if it's not "204",it's long distance
		if(numDigits == 7){
		  System.out.println("User entered: "+strInput);
		  System.out.println("Phone number parsed: ");
		  System.out.println("Valide phone number: ");
		}else if(numDigits == 10){
		  System.out.println("User entered: "+strInput);
		  System.out.println("Phone number parsed: ");
		  System.out.println("Area code: ");
		  System.out.println("Valide phone number: ");
		}else if(numDigits == 11){
		  System.out.println("User entered: "+strInput);
		  System.out.println("Phone number parsed: ");
		  System.out.println("Area code: ");
		  System.out.println("Valide phone number: ");
		}else{
		  System.out.println("User entered: "+strInput);
		  System.out.println("No Phone number parsed, invalid!");
		}//end of if else if
		
		  
		
	  }//end of while loops of "isDigits"
	  System.out.println("User entered: "+strInput);
	  System.out.println("No Phone number parsed, invalid!");
	  
	  
	  
	  //strInput = JOptionPane.showInputDialog("Please enter a phone number or press q to quit the program!");
	}//end of while of "enter q to quit"
	  System.out.println("Please enter a valid phone number!!");
  }//end of main
}

This post has been edited by lordms12: 07 June 2008 - 06:38 AM

Was This Post Helpful? 0
  • +
  • -

#6 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: Need Help for java homework, "phone number validation"

Posted 07 June 2008 - 07:35 AM

:D :D yes that is still java.. its called regular expression..


Your new code compiles fine.... doesn't encounter any error here.. :blink:
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Need Help for java homework, "phone number validation"

Posted 07 June 2008 - 11:02 AM

Keep it simple do it that way:

// init phone number
int nb = 0;
// convert string read into an array of char
char[] digit = strInput.toCharArray();
// loop through the digit
for(int i = 0; i < digit.length; i++) {
	// discard the invalid values
	if(digit[i] < '0') continue;
	if(digit[i] > '9') continue;
	// add to what I already have
	nb *= 10;
	nb += (digit[i] - '0';
}


Was This Post Helpful? 1
  • +
  • -

#8 colinwsun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-June 08

Re: Need Help for java homework, "phone number validation"

Posted 07 June 2008 - 09:52 PM

thanks veryone , it help,

to mensahero: i am so confused, I have input my code under another computer, it's still keep telling me the same thing
Was This Post Helpful? 0
  • +
  • -

#9 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: Need Help for java homework, "phone number validation"

Posted 08 June 2008 - 12:11 AM

See it does compile.. it runs.. no error..

Posted Image
Posted Image



import javax.swing.*;
public class sun_wenqian_A2Q1{
  public static void main(String args[]){
    
    //declare variables within main
    String strInput;
    String digits;
    
    
    //asking user to enter input
    strInput = JOptionPane.showInputDialog("Please enter a phone number or press q to quit the program!");

    while(strInput.equals("q")){
      digits = strInput.replace("(","").replace(")","").replace("-","");
      
      int i = 0; // declare i for loops
      int numDigits;
      while(Character.isDigit(digits.charAt(i))){
        i++;
        numDigits = Integer.parseInt(digits);
        
        //4 kind of Phone numbers:nnnnnn,nnnnnnnnnn,nnnnnnnnnnn,if it's not "204",it's long distance
        if(numDigits == 7){
          System.out.println("User entered: "+strInput);
          System.out.println("Phone number parsed: ");
          System.out.println("Valide phone number: ");
        }else if(numDigits == 10){
          System.out.println("User entered: "+strInput);
          System.out.println("Phone number parsed: ");
          System.out.println("Area code: ");
          System.out.println("Valide phone number: ");
        }else if(numDigits == 11){
          System.out.println("User entered: "+strInput);
          System.out.println("Phone number parsed: ");
          System.out.println("Area code: ");
          System.out.println("Valide phone number: ");
        }else{
          System.out.println("User entered: "+strInput);
          System.out.println("No Phone number parsed, invalid!");
        }//end of if else if
        
          
        
      }//end of while loops of "isDigits"
      System.out.println("User entered: "+strInput);
      System.out.println("No Phone number parsed, invalid!");
      
      
      
      //strInput = JOptionPane.showInputDialog("Please enter a phone number or press q to quit the program!");
    }//end of while of "enter q to quit"
      System.out.println("Please enter a valid phone number!!");
  }//end of main
}




Thats your code that I've been trying.. it does compile.. :blink:
Was This Post Helpful? 0
  • +
  • -

#10 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: Need Help for java homework, "phone number validation"

Posted 09 June 2008 - 12:44 PM

View Postmensahero, on 8 Jun, 2008 - 12:11 AM, said:

See it does compile.. it runs.. no error..

Posted Image
Posted Image



import javax.swing.*;
public class sun_wenqian_A2Q1{
  public static void main(String args[]){
    
    //declare variables within main
    String strInput;
    String digits;
    
    
    //asking user to enter input
    strInput = JOptionPane.showInputDialog("Please enter a phone number or press q to quit the program!");

    while(strInput.equals("q")){
      digits = strInput.replace("(","").replace(")","").replace("-","");
      
      int i = 0; // declare i for loops
      int numDigits;
      while(Character.isDigit(digits.charAt(i))){
        i++;
        numDigits = Integer.parseInt(digits);
        
        //4 kind of Phone numbers:nnnnnn,nnnnnnnnnn,nnnnnnnnnnn,if it's not "204",it's long distance
        if(numDigits == 7){
          System.out.println("User entered: "+strInput);
          System.out.println("Phone number parsed: ");
          System.out.println("Valide phone number: ");
        }else if(numDigits == 10){
          System.out.println("User entered: "+strInput);
          System.out.println("Phone number parsed: ");
          System.out.println("Area code: ");
          System.out.println("Valide phone number: ");
        }else if(numDigits == 11){
          System.out.println("User entered: "+strInput);
          System.out.println("Phone number parsed: ");
          System.out.println("Area code: ");
          System.out.println("Valide phone number: ");
        }else{
          System.out.println("User entered: "+strInput);
          System.out.println("No Phone number parsed, invalid!");
        }//end of if else if
        
          
        
      }//end of while loops of "isDigits"
      System.out.println("User entered: "+strInput);
      System.out.println("No Phone number parsed, invalid!");
      
      
      
      //strInput = JOptionPane.showInputDialog("Please enter a phone number or press q to quit the program!");
    }//end of while of "enter q to quit"
      System.out.println("Please enter a valid phone number!!");
  }//end of main
}




Thats your code that I've been trying.. it does compile.. :blink:

ya it compiles even i checked good work
Was This Post Helpful? 0
  • +
  • -

#11 colinwsun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-June 08

Re: Need Help for java homework, "phone number validation"

Posted 09 June 2008 - 03:49 PM

thanks man! it's so weird. gonna try compile my code on library machine tonight.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1