search

Home  >  Q&A  >  body text

Java outputs each character in a string containing commas

    public void evaluateCourse() {
//        Res res = new Res();
//        Connection con = null;
        String total = "1,2,33,53";
        for(int i = 0;i < total.length() ; i ++){
            if(total.charAt(i) != ','){
                System.out.println(total.charAt(i));
            }
        }
    }

The output result is

The result I want is: 1

            2
            33
            53
PHP中文网PHP中文网2731 days ago996

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-06-28 09:26:45

    Don’t use it yetString.split()

        public void evaluateCourse() {
    //        Res res = new Res();
    //        Connection con = null;
            String total = "1,2,33,53";
            String temp = "";
            for(int i = 0;i < total.length() ; i ++){
                temp += total.charAt(i);
                if(total.charAt(i) == ','){
                    System.out.println(temp);
                    temp = "";
                }
            }
        }

    Look at using split again

    public void evaluateCourse() {
    //        Res res = new Res();
    //        Connection con = null;
            String total = "1,2,33,53";
            String[] temp = aa.split(",");
            for(String s : temp) {
                System.out.println(s);
            }
        }

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-28 09:26:45

    Why not use String.split()?

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-28 09:26:45

    Stream.of(total.split(",")).forEach(System.out::println);

    reply
    0
  • Cancelreply