【问题标题】:Program not reading first String input程序未读取第一个字符串输入
【发布时间】:2018-12-08 01:23:09
【问题描述】:

在网上搜索了大约 30 分钟,但在任何地方都找不到此错误。所以,基本上我有一种方法可以在矩阵中找到某人的电子邮件,以便能够更改它。问题是,当我输入案例 1 时,我的程序在第二次编写时只读取“新电子邮件:”的输入。

public static boolean atualizarInfoParticipante(String email,int nEl,String [][]equipas){

    int pos=Utilitários.pesquisarElemento1(email,nEl,equipas);
    String email2;
    if(pos!=-1){
        int op;

        do{
            Formatter out=new Formatter(System.out);
            Utilitários.mostrarParticipante(out,equipas[pos]);
            //op=Utilitários.menuParticipante(email);
            System.out.println();
            System.out.println();
            System.out.printf("UPDATE USER INFO ",email);
            System.out.printf("\n");
            System.out.println("email - 1");
            System.out.println("Name - 2");
            System.out.println("Birth Date - 3");
            System.out.println("End - 0");
            op=ler.nextInt();
            ler.nextLine();

            switch(op){
                case 1: 
                        System.out.print("New email:");
                        email2=ler.nextLine();
                        if(Utilitários.pesquisarElemento1(email2,nEl,equipas)==-1){

                            equipas[pos][0]=ler.nextLine();
                        }
                        else{System.out.println("Email já existente. Alteração inválida.");}
                        break;}

这只是部分情况,唯一有错误的情况。

输出:

   ac@abc.pt;   Ana Costa;  22/11/1999;    The Best;

UPDATE USER INFO
email - 1
Name - 2
Birth Date - 3
End - 0
1
New email:sss@abc.pt

        ;   Ana Costa;  22/11/1999;    The Best;

我已经尝试过无数次清除缓冲区,但没有任何改变。

【问题讨论】:

    标签: java string input


    【解决方案1】:

    我认为这是因为您阅读了两次电子邮件。

    System.out.print("New email:");
    email2=ler.nextLine();
    

    您在此处阅读电子邮件并在此处再次阅读:

    if(Utilitários.pesquisarElemento1(email2,nEl,equipas)==-1){
    
        equipas[pos][0]=ler.nextLine();
    }
    

    我的理解是,如果电子邮件不存在,您将阅读它两次。 请把你要读的班级的名字也写上。

    【讨论】:

    • 没错,我错了。你解决了我的问题,谢谢!
    • 我不是巴西人,我是葡萄牙人 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2022-11-30
    • 2016-08-16
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多