英文:
My code keeps on Printing " [Ljava.lang.String;@4e50df2e "not really sure where to and how to put this toString()
问题
import java.util.Scanner;
class Stur {
static String branchcode = "C04";
static String branch = "Computer Sc. & Eng. ";
static String rollcode = "18020";
static String collegeName = "Kalaniketan Polytechnic College";
String rollno[], name[];
Stur(String n[], String r[]) {
rollno = r;
name = n;
System.out.println(name + " \t " + rollcode + rollno + " \t" + branch + collegeName);
}
}
class staticVar1 { // main method class
public static void main(String ujyg[]) {
int j = 0, i = 0;
Scanner object = new Scanner(System.in);
System.out.println("how many Students ");
int n = object.nextInt();
String arr[] = new String[n];
for (i = 0; i < arr.length; i++) {
System.out.print("Enter roll number:");
arr[i] = object.next();
System.out.print("Enter Name:");
arr[i] = object.next();
}
System.out.println("Name \t Roll Number \t Branch \t\t College\n");
for (j = 0; j < arr.length; j++) {
Stur st = new Stur(new String[i], new String[i]);
}
}
}
期望的输出如下所示
Name Roll Number Branch College
Anf 18020C0406 Computer Sc. & Eng. Kalaniketan Polytechnic College
xyz 18020C0402 Computer Sc. & Eng. Kalaniketan Polytechnic College
Ad 18020C0405 Computer Sc. & Eng. Kalaniketan Polytechnic College
Ax 18020C0401 Computer Sc. & Eng. Kalaniketan Polytechnic College
英文:
** as you can see I have made a program to print Student Record of same branch and college in which user inputs the name and roll number.
I have tried generating toString() from my IDE but it is still showing " [Ljava.lang.String;@4e50df2e" these...please help me get rid of this...thank You.**
class Stur
{
static String branchcode="C04";
static String branch="Computer Sc. & Eng. ";
static String rollcode="18020";
static String collegeName="Kalaniketan Polytechnic College";
String rollno[],name[];
Stur(String n[],String r[])
{
rollno=r;
name=n;
System.out.println(name+" \t "+rollcode+""+branchcode+""+rollno+" \t"+branch+""+collegeName);
}
}
class staticVar1 //main method class
{
public static void main(String ujyg[])
{
int j=0, i=0;
Scanner object=new Scanner(System.in);
System.out.println("how many Students ");
int n=object.nextInt();
String arr[]=new String [n];
for(i=0;i<arr.length;i++)
{
System.out.print("Enter roll number:");
arr[i]=object.next();
System.out.print("Enter Name:");
arr[i]=object.next();
}
System.out.println("Name \t Roll Number \t Branch \t\t College\n");
for(j=0;j<arr.length;j++)
{
Stur st=new Stur(new String[i], new String[i]);
}}
}
Expecting output like this
Name Roll Number Branch College
Anf 18020C0406 Computer Sc. & Eng. Kalaniketan Polytechnic College
xyz 18020C0402 Computer Sc. & Eng. Kalaniketan Polytechnic College
Ad 18020C0405 Computer Sc. & Eng. Kalaniketan Polytechnic College
Ax 18020C0401 Computer Sc. & Eng. Kalaniketan Polytechnic College
</details>
# 答案1
**得分**: 1
请找到更新的代码。
```java
import java.util.Scanner;
class Stur {
static String branchcode = "C04";
static String branch = "计算机科学与工程";
static String rollcode = "18020";
static String collegeName = "卡拉尼克坦理工学院";
String rollno, name;
Stur(String n, String r) {
rollno = r;
name = n;
System.out.println(name + "\t" + rollcode + branchcode + rollno + "\t" + branch + "\t" + collegeName);
}
}
class StaticVar1 { //main method class
public static void main(String ujyg[]) {
int j = 0, i = 0;
Scanner object = new Scanner(System.in);
System.out.println("有多少名学生");
int n = object.nextInt();
String nameArr[] = new String[n];
String rollnoArr[] = new String[n];
for (i = 0; i < n; i++) {
System.out.print("输入学号:");
rollnoArr[i] = object.next();
System.out.print("输入姓名:");
nameArr[i] = object.next();
}
System.out.println("姓名\t学号\t\t专业\t\t学院\n");
Stur st[] = new Stur[n];
for (j = 0; j < n; j++) {
st[j] = new Stur(nameArr[j], rollnoArr[j]);
}
}
}
您之前尝试直接打印数组,因此它会给您对象哈希代码和类名。如果您希望输出如您所述,您需要逐个打印数组的元素。
英文:
Please find updated code.
class Stur
{
static String branchcode="C04";
static String branch="Computer Sc. & Eng. ";
static String rollcode="18020";
static String collegeName="Kalaniketan Polytechnic College";
String rollno,name;
Stur(String n,String r)
{
rollno=r;
name=n;
System.out.println(name + "\t" + rollcode+branchcode+rollno + "\t" + branch + " " + collegeName);
}
}
class staticVar1 //main method class
{
public static void main(String ujyg[])
{
int j=0, i=0;
Scanner object=new Scanner(System.in);
System.out.println("how many Students ");
int n=object.nextInt();
String nameArr[]=new String [n];
String rollnoArr[]=new String [n];
for(i=0;i<n;i++)
{
System.out.print("Enter roll number:");
rollnoArr[i]=object.next();
System.out.print("Enter Name:");
nameArr[i]=object.next();
}
System.out.println("Name \t Roll Number \t Branch \t\t College\n");
Stur st[] = new Stur[n];
for(j=0;j<n;j++)
{
st[j] = new Stur(nameArr[j], rollnoArr[j]);
}
}
}
You were trying to print an array directly, so it was giving you object hash code and class name. If you want output as you have mentioned, you need to print the element of the array one by one.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论