gpt4 book ai didi

java - 无法打印变量的值

转载 作者:行者123 更新时间:2023-11-29 07:25:46 25 4
gpt4 key购买 nike

在java中打印变量的值似乎很简单,但我无法正确执行此操作。我有 Mysql 表,其中包含名字“fname”和姓氏“lname”。连接到我的 sql 后,我获取这些值并将其存储在变量中。然后问题开始了...这是 screenshot这是我的代码

package signup;

import java.sql.*;
import java.util.Random;
import org.openqa.selenium.Keys;

public class Signup {
private static final String db_connect = "jdbc:mysql://localhost/test1" ;
private static final String uname = "username" ;
private static final String pass = "password" ;

private Connection conMethod(){
Connection conVar = null;
try{Class.forName("com.mysql.jdbc.Driver");conVar = DriverManager.getConnection(db_connect,uname,pass);}catch(SQLException e){e.printStackTrace();
}catch(ClassNotFoundException e){e.printStackTrace();}return conVar;}

public void selectMethod(){Statement query = null;
ResultSet rs1 = null;
Connection conVar2= conMethod();try{query = conVar2.createStatement();

rs1 = query.executeQuery("Select * from fnames2");
String[] fname=new String[500]; String[] lname=new String[500];
int a=0;while(rs1.next()){fname[a]=rs1.getString(2); lname[a]=rs1.getString(3); a++;}

String firstname = fname[1];
String lastname = lname[1];
String fullname = firstname+" "+lastname;
String email = firstname+lastname+"@yahoo.com";


System.out.println("first name is "+firstname);
System.out.println("last name is "+lastname);
System.out.println("full name is "+fullname);
System.out.println("email is "+email);


} catch(SQLException e){e.printStackTrace();}

}

public static void main (String args[]){Signup obj = new Signup();obj.selectMethod();}
}

这是它的输出

first name is PATRICIA
last name is ALISHA
full name is PATRICIA ALISHA
@yahoo.comATRICIAALISHA

您可以看到问题出在电子邮件变量中。它应该打印 PATRICIAALISHA@yahoo.com 但它正在打印“@yahoo.comATRICIAALISHA”。谢谢

最佳答案

输出与 lastname 一致,为 “ALISHA\r”。发生的情况是,当您打印它时(取决于您的操作系统),\r 字符会使光标返回到行的开头。在打印 "last name is""full name is" 的情况下,这对输出的外观没有影响,因为光标只会转到无论如何下一行。但它会导致 email"PATRICIAALISHA\r@yahoo.com",这意味着它输出 email is PATRICIAALISHA 后,光标会移动回到行首并用 @yahoo.com 覆盖已有内容,这足以覆盖 P 向上的文本。

关于java - 无法打印变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34605643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com