I can't able to print both the arrays at the same time it always come out with
[Jeno, Jaemin, Jaehyun][78, 59, 89][Passed, Failed, Passed]
I wanted it to print out like this
Jeno - 78 - Passed
Jaemin - 59 - Failed
Jaehyun - 89 - Passed
Source code:
import java.util.Arrays;
public class PracticeArray {
public static int [] arr5 = {78,58,89};
public static String names[] = {"Jeno", "Jaemin", "Jaehyun"};
public static String result[] = {"Passed", "Failed", " Passed"};
public static void main (String [] args)
{
System.out.println(Arrays.toString(names) " - " Arrays.toString(arr5) " - " Arrays.toString(result));
}
}
CodePudding user response:
The easiest way to do this might be to just use an explicit loop:
int [] arr5 = {78,58,89};
String names[] = {"Jeno", "Jaemin", "Jaehyun"};
String result[] = {"Passed", "Failed", "Passed"};
for (int i=0; i < arr5.length; i) {
System.out.println(names[i] " - " arr5[i] " - " result[i]);
}
This prints:
Jeno - 78 - Passed
Jaemin - 58 - Failed
Jaehyun - 89 - Passed
CodePudding user response:
I'd say that it depends on what exactly you're trying to do here. If your goal is to just print out the results, Tim's way will work. But its very short sighted and not really structured in any way. Just goofing around here but this is what I came up with in a pinch cause I was bored.
package driver;
public class Person
{
private String name;
private int score;
private String result;
public Person(String name, int score)
{
this.name = name;
this.score = score;
if (this.score < 60)
{
Person.this.setResult("Failed");
}
else
{
Person.this.setResult("Passed");
}
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getScore()
{
return score;
}
public void setScore(int score)
{
this.score = score;
}
public String getResult()
{
return result;
}
public void setResult(String result)
{
this.result = result;
}
}
package driver;
import java.util.Scanner;
public class Driver
{
public static int counter = 0;
public static void output(Person[] people)
{
for (int i = 0; i < people.length; i )
{
System.out.println(people[i].getName() " - " people[i].getScore() " - " people[i].getResult());
}
}
public static void getInfo(Person[] people)
{
Scanner scnr = new Scanner(System.in);
String name = "";
int score = 0;
System.out.println("Enter name: ");
name = scnr.next();
System.out.println("Enter score: ");
score = scnr.nextInt();
Person person = new Person(name, score);
people[counter] = person;
counter ;
}
public static void main(String[] args)
{
Scanner scnr = new Scanner(System.in);
System.out.println("How many entries do you wish to add?");
int entries = scnr.nextInt();
Person people[] = new Person[entries];
for (int i = 0; i < entries; i )
{
getInfo(people);
}
output(people);
scnr.close();
}
}
This will allow you to enter however many people and scores you'd like and you wont have to try and mess with three individual arrays. The issue doing it your way seems to be that you have to manage 3 individual arrays and ensure that each has the correct values at each and then managing them and accessing all three to output values. It can get messy to do it that way. Or you'd have to possibly do multi dimensional arrays. So it seems to me a single array of Person objects is better. Anyway, I'm learning but this is what I came up with tinkering with it.
OUTPUT:
Jeno - 78 - Passed
Jaemin - 58 - Failed
Jaehyun - 89 - Passed