1 Reply Latest reply: Dec 16, 2009 11:04 AM by 800560 RSS

    .substring issues

    843810
      hi, i am learning java in class for school, and in my project i have one issue with the .substring method of strings.

      my code is
      import java.util.Scanner;
      
      public class Valid
      {
      
           private String lastName;
           private String email;
           private boolean oneSymbol;
           private boolean nonBlankLeft;
           private boolean onePeriod;
           private boolean nonBlankBoth;
      
      
           Valid()
           {}
      
           public void readData(Scanner input)
           {
                lastName = input.next();
                email = input.next();
           }
      
           public void checkSymbol()
           {
                String s = email.substring(0);
                int count = 0;
                int x;
                x = s.indexOf('@');
                while (!(x == -1))
                {
                     s = s.substring(0,x) = s.substring(x+1);
                     count++;
                     x = s.indexOf('@');
                }
                if (count == 1)
                     oneSymbol = true;
                else
                     oneSymbol = false;
           }
      
           public void checkNonBlankLeft()
           {
                int x = email.indexOf(" @");
                if (x == -1)
                     nonBlankLeft = true;
                else
                     nonBlankLeft = false;
           }
      
           public void checkOnePeriod()
           {
                String s = email.substring(0);
                int count = 0;
                int x;
                x = s.indexOf('.');
                while (!(x == -1))
                {
                     s = s.substring(0,x) = s.substring(x+1);
                     count++;
                     x = s.indexOf('.');
                }
                if (count >= 1)
                     onePeriod = true;
                else
                     onePeriod = false;
           }
      
           public void checkNonBlank()
           {
                int x = email.indexOf(" . ");
                if (x == -1)
                     nonBlankBoth = true;
                else
                     nonBlankBoth = false;
           }
      
           public void writeResults(java.io.PrintWriter outputValid, java.io.PrintWriter outputInvalid)
           {
      
                if (oneSymbol && nonBlankLeft && onePeriod && nonBlankBoth)
                     outputValid.printf("%-15s%-30s", lastName, email + "\n");
                else
                {
                     outputInvalid.printf("%-15s%-30s", lastName, email + "\n");
                     if (oneSymbol == false)
                          outputInvalid.printf("%45s%-30s", " ", "@ symbol must occur once\n");
                     if (nonBlankLeft == false)
                          outputInvalid.printf("%45s%-30s", " ", "Must be a character before the @ symbol\n");
                     if (onePeriod == false)
                          outputInvalid.printf("%45s%-30s", " ", "Must contain one . (period)\n");
                     if (nonBlankBoth == false)
                          outputInvalid.printf("%45s%-30s", " ", "Both sides of . must be contain a character\n");
                }
           }
      }
      i have issues with both .substring methods and i get these errors.

      .\Valid.java:31: unexpected type
      required: variable
      found : value
                     s = s.substring(0,x) = s.substring(x+1);
                     ^
      .\Valid.java:58: unexpected type
      required: variable
      found : value
                     s = s.substring(0,x) = s.substring(x+1);
                     ^
      2 errors
        • 1. Re: .substring issues
          800560
          This line of code and the other similar line are not valid syntax for Java.
          s = s.substring(0,x) = s.substring(x+1);
          What did you intend for that line to do?

          I think your checkSymbol method is supposed to return true if the email string contains exactly 1 @ symbol. I suggest your re-write it and use the charAt method of String. Here's some pseudo code.

          count = 0
          For each char of the String , if the char is an @, increment count.
          After looping, return count == 1