Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jim829

Thread: String printing problem

  1. #1
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    32
    Rep Power
    0

    Default String printing problem

    Hello all,
    This program -mainly- takes a phone number and adds 2 to each number and format it then out put it.
    From line 11 to line 71 for inputs a valid phone number, there is no problem on it.
    From line 72 to line 109 for adding 2 to each digit, there is no problems on it.

    My problem is from line 110, it prints the number right, but when I tries to format the string to print it in the desired format, the first three indexes in the string printed in different values.
    the index 0 is 2 as printed in line 110 but it is 1 in line 113 and 114
    the index 1 is 1 as printed in line 110 but it is 4 in line 113 and 114
    the index 2 is 2 as printed in line 110 but it is 9 in line 113 and 114

    I do not know what is the problem?

    Java Code:
    import java.util.Scanner;
    
    public class Exe614972 {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String s = "";
            boolean invalid = true;
            int digit = 0;
            do {
                digit = 0;
                System.out.println("Enter a telephone number:");
                s = scan.nextLine();
                for (int counter = 0; counter <= s.length() - 1; counter++) {
                    switch (s.charAt(counter)) {
                        case '9':
                            digit++;
                            invalid = false;
                            break;
                        case '0':
                            digit++;
                            invalid = false;
                            break;
                        case '1':
                            digit++;
                            invalid = false;
                            break;
                        case '2':
                            digit++;
                            invalid = false;
                            break;
                        case '3':
                            digit++;
                            invalid = false;
                            break;
                        case '4':
                            digit++;
                            invalid = false;
                            break;
                        case '5':
                            digit++;
                            invalid = false;
                            break;
                        case '6':
                            digit++;
                            invalid = false;
                            break;
                        case '7':
                            digit++;
                            invalid = false;
                            break;
                        case '8':
                            digit++;
                            invalid = false;
                            break;
                        case '-':
                            invalid = false;
                            break;
                        case ' ':
                            invalid = false;
                            break;
                        default:
                            invalid = true;
                            counter = s.length();
                            break;
                    }
                }
                if (digit > 10 || digit <= 0) {
                    invalid = true;
                }
            } while (invalid);
            String x = "";
            for (int counter = 0; counter <= s.length() - 1; counter++) {
                switch (s.charAt(counter)) {
                    case '0':
                        x += "2";
                        break;
                    case '1':
                        x += "3";
                        break;
                    case '2':
                        x += "4";
                        break;
                    case '3':
                        x += "5";
                        break;
                    case '4':
                        x += "6";
                        break;
                    case '5':
                        x += "7";
                        break;
                    case '6':
                        x += "8";
                        break;
                    case '7':
                        x += "9";
                        break;
                    case '8':
                        x += "0";
                        break;
                    case '9':
                        x += "1";
                        break;
                    default:
                        break;
                }
    
            }
            System.out.println(x);
    
            String m = x.charAt(0) + x.charAt(1) + x.charAt(2) + "-" + x.charAt(3) + x.charAt(4) + x.charAt(5) + "-" + x.charAt(6) + x.charAt(7) + x.charAt(8) + x.charAt(9);
            System.out.println(m);
            System.out.println(x.charAt(0) + x.charAt(1) + x.charAt(2) + "-" + x.charAt(3) + x.charAt(4) + x.charAt(5) + "-" + x.charAt(6) + x.charAt(7) + x.charAt(8) + x.charAt(9));
        }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String printing problem

    char are arithmetic values. An expression: char1 + char2 will add the two values together instead of creating a String with the two chars.
    If you don't want the two char values to be added together, create a String by putting an empty String ("") between the two char values.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    32
    Rep Power
    0

    Default Re: String printing problem

    Thank you Norm,
    please give me an example code.
    how can I put "" between the two char values?

    Thank you

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String printing problem

    To build a String to print, use the concatenation operator: + between each value you want in the String:
    For example: part1 + part2 +part3 + part4 etc
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: String printing problem

    Probably the easiest way is to prefix an empty String ("") to the beginning of the print or assignment statement.

    String m = "" + x.char(0) +x.char(1) .. continue as you already did in your code.

    Regards,
    Jim
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    32
    Rep Power
    0

    Default Re: String printing problem

    Thank you Jim this was useful.

    Thank you all

Similar Threads

  1. Printing Problem
    By PROPA in forum New To Java
    Replies: 7
    Last Post: 11-03-2011, 02:40 PM
  2. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 09:17 AM
  3. Printing problem
    By justlynn in forum NetBeans
    Replies: 5
    Last Post: 08-03-2010, 02:41 PM
  4. printing an array of String through drawString
    By kaemonsaionji in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 04:38 PM
  5. Problem after Printing GUI.
    By coldblood22 in forum AWT / Swing
    Replies: 1
    Last Post: 04-05-2008, 02:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •