My code keeps on Printing " [Ljava.lang.String;@4e50df2e "not really sure where to and how to put this toString()

huangapple go评论91阅读模式
英文:

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=&quot;C04&quot;;
static String branch=&quot;Computer Sc. &amp; Eng. &quot;;
static String rollcode=&quot;18020&quot;;
static String collegeName=&quot;Kalaniketan Polytechnic College&quot;;
String rollno[],name[];
Stur(String n[],String r[])
{
rollno=r;
name=n;
System.out.println(name+&quot; \t &quot;+rollcode+&quot;&quot;+branchcode+&quot;&quot;+rollno+&quot; \t&quot;+branch+&quot;&quot;+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(&quot;how many Students &quot;);
int n=object.nextInt();
String arr[]=new String [n];
for(i=0;i&lt;arr.length;i++)
{
System.out.print(&quot;Enter roll number:&quot;);
arr[i]=object.next();
System.out.print(&quot;Enter Name:&quot;);
arr[i]=object.next();
}
System.out.println(&quot;Name \t Roll Number \t   Branch \t\t College\n&quot;);
for(j=0;j&lt;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. &amp; Eng. Kalaniketan Polytechnic College 
xyz   18020C0402     Computer Sc. &amp; Eng. Kalaniketan Polytechnic College
Ad    18020C0405     Computer Sc. &amp; Eng. Kalaniketan Polytechnic College 
Ax    18020C0401     Computer Sc. &amp; 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=&quot;C04&quot;;
static String branch=&quot;Computer Sc. &amp; Eng. &quot;;
static String rollcode=&quot;18020&quot;;
static String collegeName=&quot;Kalaniketan Polytechnic College&quot;;
String rollno,name;
Stur(String n,String r)
{
rollno=r;
name=n;
System.out.println(name + &quot;\t&quot; + rollcode+branchcode+rollno + &quot;\t&quot; + branch + &quot; &quot; + 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(&quot;how many Students &quot;);
int n=object.nextInt();
String nameArr[]=new String [n];
String rollnoArr[]=new String [n];
for(i=0;i&lt;n;i++)
{
System.out.print(&quot;Enter roll number:&quot;);
rollnoArr[i]=object.next();
System.out.print(&quot;Enter Name:&quot;);
nameArr[i]=object.next();
}
System.out.println(&quot;Name \t Roll Number \t   Branch \t\t College\n&quot;);
Stur st[] = new Stur[n];
for(j=0;j&lt;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.

huangapple
  • 本文由 发表于 2020年9月15日 12:34:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63895153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定