When I run it says: null // RELATIONS 1 - 1 [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am finishing up my program to generate a receipt
My program reads three facts, lecturaAnterior, lecturaActual and stratum.
After doing that tells me nulll
What could it be?
Thank you for your contributions.
CLASS ONE
public class Suscriptor {
private int nuip;
private String nombre;
private String apellido;`
public Suscriptor(int nuip, String nombre, String apellido) {
this.nuip = nuip;
this.nombre = nombre;
this.apellido = apellido;
}
public int getNuip() {
return nuip;
}
public String getNombre() {
return nombre;
}
public String getApellido() {
return apellido;
}
}
CLASS TWO
public class Vivienda {
private String direccion;
private String barrio;
private String ciudad;
private byte estrato;`
public Vivienda(String direccion, String barrio, String ciudad, byte estrato) {
this.direccion = direccion;
this.barrio = barrio;
this.ciudad = ciudad;
this.estrato = estrato;
}
public String getDireccion() {
return direccion;
}
public String getBarrio() {
return barrio;
}
public String getCiudad() {
return ciudad;
}
public byte getEstrato() {
return estrato;
}
CLASS THREE
public class Recibo {
public long lecturaAnterior;
public long lecturaActual;
public Vivienda viviend;
public Suscriptor suscrip;`
public Recibo(long lecturaAnterior, long lecturaActual)
throws Exception {
if (lecturaAnterior <= 0.0F ){
Exception e = new Exception ("La lectura anterior no puede ser menor a cero");
throw e;
}
this.lecturaAnterior = lecturaAnterior;
this.lecturaActual = lecturaActual;
this.viviend = viviend;
this.suscrip = suscrip;
}
public long getLecturaAnterior() {
return lecturaAnterior;
}
public long getLecturaActual() {
return lecturaActual;
}
public Vivienda getViviend (){
return viviend ;
}
public Suscriptor getSuscrip () {
return suscrip ;
}
public long Consumo (){
return ( this.lecturaActual - this.lecturaAnterior );
}
public long cargoBasico (){
if ( this.viviend.getEstrato() == 1 || this.viviend.getEstrato() == 2) {return 2000 ;}
if (this.viviend.getEstrato() == 3) { return 5000 ; }
if (this.viviend.getEstrato() == 4 || this.viviend.getEstrato() == 5 ) { return 5000; }
if (this.viviend.getEstrato() == 6 || this.viviend.getEstrato() == 7 ) { return 12000 ; }
return 0;
}
public long ValorConsumo () {
return ( this.Consumo() * 250 ) ;
}
MAIN
import java.util.Scanner;`
/**
*
* #author christian1
*/
public class Programa {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
Scanner scan = new Scanner (System.in);
System.out.println("INGRESE LA LECTURA ANTERIOR : ");
long lecturaAnterior = scan.nextLong();
System.out.println("INGRESE LA LECTURA ACTUAL : ");
long lecturaActual = scan.nextLong();
System.out.println("INGRESE EL ESTRATO: ");
byte estrato = scan.nextByte();
Vivienda V1 = new Vivienda ("Av Junin 65-97","Rep De Israel","Villa de las Palmas",estrato );
Suscriptor S1 = new Suscriptor (1113587452,"Juan","Roman");
Recibo R1 = new Recibo (lecturaAnterior,lecturaActual);
System.out.println ("PAGUE PORFAVOR: SEÑOR ... " + S1.getNombre()+"QUE VIVE EN "+ V1.getDireccion() + R1.ValorTotal() + " A CONTINUACION DISCRIMINAMOS SU CONSUMO" + R1.ValorConsumo() + R1.Incremento() + R1.Descuento() + R1.ValorIva() + R1.ValorTotal());
} catch (Exception e){
System.out.println(e.getMessage());
}
} }

As ZouZou suggested you could have find the problem if you debug. Or read the exception may be.
In class Recibo you don't set the values for
Vivienda viviend
Suscriptor suscrip
In the constructor you just assigning them to self. Create those and pass it to constructor as you do for lecturaAnterior, lecturaActual or use a setter.

Related

Cannot access a method call from non-static to non-static class/method

I am terribly sorry to have to bother you all with this silly question since I should really know this by now but here goes:
public class Toets
private String vak;
/**
* Constructor for objects of class Toets
*/
public Toets(String toetsVak)
{
vak = toetsVak;
new Vraag();
}
/**
* An example of a method - replace this comment with your own
*
* #param y a sample parameter for a method
* #return the sum of x and y
*/
public void addVraag(int vraagID, String vraag, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord)
{
new Vraag(vraagID, vraag, antwoordA, antwoordB, antwoordC, correcteAntwoord);
}
public void printVraag(int vraagID)
{
Vraag.printVraag(vraagID);
}
which should be called from here:
public class Vraag
private int vraagID;
private String vraag;
private String antwoordA;
private String antwoordB;
private String antwoordC;
private String correcteAntwoord;
/**
* Constructor for objects of class Vraag
*/
public Vraag()
{
vraagID = 0;
vraag = "";
antwoordA = "";
antwoordB = "";
antwoordC = "";
correcteAntwoord = "";
}
/**
* Constructor for objects of class Vraag
*/
public Vraag(int vraagID, String vraag, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord)
{
this.vraagID = vraagID;
this.vraag = vraag;
this.antwoordA = antwoordA;
this.antwoordB = antwoordB;
this.antwoordC = antwoordC;
this.correcteAntwoord = correcteAntwoord;
}
public void printVraag(int vraagID)
{
if(this.vraagID == vraagID)
{
System.out.println("Vraag " + vraagID + ".");
System.out.println(vraag);
System.out.println("A. " + antwoordA);
System.out.println("B. " + antwoordB);
System.out.println("C. " + antwoordC);
}
}
}
The thing I am trying to do is create a Test for a school and add questions one by one and print a question in accordance with the designated ID (AKA question number); we are not allowed to use statics so I have looked at ways to accomplish this but I kind of lose it at the Java.lang.reflection stuff.
The thing is that I need to be able to create multiple instances of Vraag and call the print method of each one which I apparently can not do at the moment.
Instead of providing Vraag as attribute, you simple provide List<Vraag>.
Everytime when you create new Vraag object simply put into list.
And while printing all attributes of particular object you can filter out record based on passed Id.
Try below code it will help you.
class Toets {
private String vak;
private List<Vraag> vraagList;
public Toets(String toetsVak) {
vak = toetsVak;
}
public void addVraag(int vraagID, String att, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord) {
vraagList.add(new Vraag(vraagID, att, antwoordA, antwoordB, antwoordC, correcteAntwoord));
}
public void printVraag(int vraagID) {
//Java 8 code
Vraag obj=vraagList.stream().filter(vraag -> vraag.getVraagID()==vraagID).collect(Collectors.toList()).get(0);
obj.printVraag(vraagID);
/* Before Java 8
for(Vraag obj:vraagList){
if(obj.getVraagID()==vraagID){
obj.printVraag(vraagID);
}
}*/
}
}
class Vraag {
private int vraagID;
private String vraag;
private String antwoordA;
private String antwoordB;
private String antwoordC;
private String correcteAntwoord;
public Vraag(int vraagID, String vraag, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord) {
this.vraagID = vraagID;
this.vraag = vraag;
this.antwoordA = antwoordA;
this.antwoordB = antwoordB;
this.antwoordC = antwoordC;
this.correcteAntwoord = correcteAntwoord;
}
public void printVraag(int vraagID) {
System.out.println("Vraag " + vraagID + ".");
System.out.println(vraag);
System.out.println("A. " + antwoordA);
System.out.println("B. " + antwoordB);
System.out.println("C. " + antwoordC);
System.out.println("correcteAntwoord. " + correcteAntwoord);
}
public int getVraagID() {
return vraagID;
}
public void setVraagID(int vraagID) {
this.vraagID = vraagID;
}
public String getVraag() {
return vraag;
}
public void setVraag(String vraag) {
this.vraag = vraag;
}
public String getAntwoordA() {
return antwoordA;
}
public void setAntwoordA(String antwoordA) {
this.antwoordA = antwoordA;
}
public String getAntwoordB() {
return antwoordB;
}
public void setAntwoordB(String antwoordB) {
this.antwoordB = antwoordB;
}
public String getAntwoordC() {
return antwoordC;
}
public void setAntwoordC(String antwoordC) {
this.antwoordC = antwoordC;
}
public String getCorrecteAntwoord() {
return correcteAntwoord;
}
public void setCorrecteAntwoord(String correcteAntwoord) {
this.correcteAntwoord = correcteAntwoord;
}
}
Question is hard to decipher for sure, but I assume you want a list/array or something to store those questions in and that can't be static?
If so, you don't need a static (guess you know what that means) object if a class that has the list isn't created multiple times, it has a list variable but that variable only exists once which I assume is what you want

How can i verify if i'm adding atributes to a list that are equal?

import entidades.*;
public class Main {
public static void main(String[] args) {
Profissional prof = new Profissional(null, null);
List<Profissional> profissional = new ArrayList<Profissional>();
Scanner sc = new Scanner(System.in);
boolean loop = true;
while(loop == true) {
String comando = sc.next().toUpperCase();
if (comando.contentEquals("RP")) {
String nomePro = sc.nextLine();
String categoriaPro = sc.nextLine();
prof.NomeVerificacao(profissional, nomePro, categoriaPro);
}
if(comando.contentEquals("SAIR")) {
break;
}
}
for(Profissional pro : profissional) {
System.out.println(pro);
This is my Main, it's running fine but i don´t think it is adding the atributes to the list and not verifying either.
i want to add the atributes to a list so i can create different objets but they can not have at least the name equal.
public class Profissional {
private String nome;
private String categoria;
public Profissional(String nome, String categoria) {
this.nome = nome;
this.categoria = categoria;
}
public void NomeVerificacao(List<Profissional> profissional ,String nome, String categoria) {
if(profissional.isEmpty() == true) {
profissional.add(new Profissional(nome, categoria));
}else {
for(Profissional pro : profissional) {
if(pro.nome.contentEquals(nome)) {
System.out.println("Já Exite esse nome");
}else {
profissional.add(new Profissional(nome, categoria));
}
}
}
}
#Override
public String toString() {
return "nome=" + nome + ", categoria=" + categoria;
}
}
this is the Profissional Class.
I'm almost there i think but the output keeps saying that the name exists even though it is the first name i'm inserting.
I ran your code on my machine and made 3 changes into it, and it's working for me now,
1)
String nomePro = sc.next();
String categoriaPro = sc.next();
2) In professional class just changed this function a bit:
public void NomeVerificacao(List<Profissional> profissional, String nome, String categoria) {
if (profissional.isEmpty() == true) {
profissional.add(new Profissional(nome, categoria));
} else {
int i = 0;
for (; i < profissional.size(); i++) {
if (profissional.get(i).nome.equals(nome)) {
System.out.println("Já Exite esse nome");
break;
}
}
if (i == profissional.size()) {
profissional.add(new Profissional(nome, categoria));
}
}
}
3) At the end of the class Main, wrote sc.close(); to close the scanner.
i/p and o/p :
1) RP
red
color
2) RP
orange
color
3) RP
orange
paint
Já Exite esse nome
4) SAIR
nome=red, categoria=color
nome=orange, categoria=color
As you can see in above i/p and o/p, nome=red and nome=orange with categoria=color are added in the list but when we tried to add the same nome=orange again but with different category as paint it didn't add it and printed the message "Já Exite esse nome".
and after entering SAIR, the toString(); printed the list content at the end. So the message will be printed only if we try to add the object with the same name again int list (not the first or any other times).
Further optimizations are possible but for now, it will work!
I can propose the following solution:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Set is a data structure that makes sure you don't have a duplicated elements
// in this case we use TreeSet structure that accepts comparator which tells that
// we need to compare elements only by professional's name
Set<Profissional> profissionals = new TreeSet<>(Comparator.comparing(Profissional::getNome));
while (true) {
String comando = sc.next().toUpperCase();
if (comando.contentEquals("RP")) {
String nomePro = sc.next();
String categoriaPro = sc.next();
// add function returns true in case the element we're going to add
// was not presented in Set structure yet. False otherwise.
boolean isNew = profissionals.add(new Profissional(nomePro, categoriaPro));
if (!isNew) {
System.out.println("Professional with name " + nomePro + " already exists");
} else {
System.out.println("Professional with name " + nomePro + " was added");
}
} else if (comando.contentEquals("SAIR")) {
break;
}
}
// just prints all professionals at the end of the program
profissionals.forEach(System.out::println);
}
public static class Profissional {
private String nome;
private String categoria;
public Profissional(String nome, String categoria) {
this.nome = nome;
this.categoria = categoria;
}
// getters and setters
#Override
public String toString() {
return "nome=" + nome + ", categoria=" + categoria;
}
}
The output will be the following:
RP
test test
Professional with name test was added
RP
test1 test1
Professional with name test1 was added
RP
test test3
Professional with name test already exists
SAIR
nome=test, categoria=test
nome=test1, categoria=test1
package javaapplication8;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class JavaApplication8 {
public static class Profissional {
private String nome;
private String categoria;
public Profissional(String nome, String categoria) {
this.nome = nome;
this.categoria = categoria;
}
}
public static void main(String[] args) {
try {
List<Profissional> profissionalList= new ArrayList<>();
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print("\r\nEnter comando:");
String comando = sc.next().toUpperCase();
if (comando.contentEquals("RP")) {
System.out.print("nome: ");
String nome = sc.next();
sc.nextLine(); // wait enter
System.out.print("categoria: ");
String categoria = sc.next();
sc.nextLine(); // wait enter
// access constructor of Profissional
Constructor profCtor = Profissional.class.getConstructor(String.class, String.class);
profCtor.setAccessible(true);
// create instance of Profissional
Profissional newItem = (Profissional) profCtor.newInstance(nome, categoria);
// avoid duplicate nome in profissionalList
boolean isExist = false;
for(Profissional pro : profissionalList) {
if(pro != null){
if(pro.nome.toLowerCase().equals(newItem.nome.toLowerCase())){
isExist = true;
break;
}
}
}
if(!isExist){
profissionalList.add(newItem );
}
}
if(comando.contentEquals("SAIR")) {
break;
}
}
for(Profissional pro : profissionalList) {
if(pro != null) {
System.out.println("nome: " + pro.nome + " categoria: " + pro.categoria);
}
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}

TextView returning java code after converting to String

i am trying to get a string out of a random number and it is returning this
Nome1: com.example.OtherActivity#3c9413b0 x com.example.OtherActivity#132c3229 :Nome2
Nome1 and Nome2 are converting good but the rest is not
My OtherActivity class is this
public class OtherActivity{
private String teamOne;
public Team(String teamOne) {
this.teamOne = teamOne;
}
public String getTeamOne() {
return teamOne;
}
public void setTeamOne(String teamOne) {
this.teamOne = teamOne;
}
}
My TeamMixer class
public class TeamMixer extends PlayerNames {
public ArrayList<Team> times = null;
public TeamMixer(ArrayList<Team> times) {
this.times = times;
}
protected String tellJoke(){
Double randomNumber = new Double(Math.random() * times.size());
Double randomNumber1 = new Double(Math.random() * times.size());
int randomNum1 = randomNumber1.intValue();
int randomNum = randomNumber.intValue();
Team time2 = times.get(randomNum);
Team time3 = times.get(randomNum1);
String timeString = String.valueOf(time3);
String timeString2 = time2.toString();
if(time2 == time3){
Double randomNumber2 = new Double(Math.random() * times.size());
int randomNum2 = randomNumber2.intValue();
Team time4 = times.get(randomNum2);
String timeString3 = String.valueOf(time4);
String tentativa = sayTeam(timeString2, timeString3);
return tentativa;
} else{
String tentativa2 = sayTeam(timeString, timeString2);
return tentativa2;
}
}
protected String sayTeam(String teams, String teams2){
String message = (getNamePlayerOne()+": " + teams + " x " + teams2 + " :" + getNamePlayerTwo());
return message;
}
}
Appreciate the help!
Override your Team class toString method, so it returns the string not the Team object:
private class Team {
String str;
public Team(String str) {
this.str = str;
}
#Override
public String toString() {
return str;
}
}

Java Beans to test a class

I was given a class DrivingTestM.java to test with a 2 classes I wrote. Here are the classes I wrote. When i run the DrivingTestM.java it gives me an error with the line:
System.out.println( question.getDescription() );
Im not sure what the error may be. Can anyone try and shed some light on this error? Thanks!
Question.java:
public class Question {
String description;
String answerA;
String answerB;
String answerC;
int correctAnswer;
int answer;
Boolean answerCorrect;
public Question(){
}
public Question(String description, String answerA, String answerB, String answerC, int correctAnswer, int answer){
this.description = description;
this.answerA = answerA;
this.answerB = answerB;
this.answerC = answerC;
this.correctAnswer = correctAnswer;
this.answer = answer;
}
public Question(String description, String answerA, String answerB, String answerC, int correctAnswer){
this.description = description;
this.answerA = answerA;
this.answerB = answerB;
this.answerC = answerC;
this.correctAnswer = correctAnswer;
}
public Boolean isAnswerCorrect() {
return answerCorrect;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAnswerA() {
return answerA;
}
public void setAnswerA(String answerA) {
this.answerA = answerA;
}
public String getAnswerB() {
return answerB;
}
public void setAnswerB(String answerB) {
this.answerB = answerB;
}
public String getAnswerC() {
return answerC;
}
public void setAnswerC(String answerC) {
this.answerC = answerC;
}
public int getCorrectAnswer() {
return correctAnswer;
}
public void setCorrectAnswer(int correctAnswer) {
this.correctAnswer = correctAnswer;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
}
DrivingTest.java:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DrivingTest {
int currentQuestionIndex = 0;
Question currentQuestion;
Boolean lastQuestion;
int score;
List<Question> q = new ArrayList<Question>();
Question quest = new Question();
public DrivingTest() throws FileNotFoundException{
File f = new File("DrivingTest.txt");
//int n = 1;
Scanner sc = new Scanner(f);
while(sc.hasNextLine()){
String desc = sc.nextLine();
String A = sc.nextLine();
String B = sc.nextLine();
String C = sc.nextLine();
String h = sc.nextLine();
int a = Integer.parseInt(h);
String blank = sc.nextLine();
q.add(new Question(desc, A,B,C,a) );
}
sc.close();
}
public void setCurrentQuestionIndex(int currentQuestionIndex) {
this.currentQuestionIndex = currentQuestionIndex;
}
public int getCurrentQuestionIndex() {
return currentQuestionIndex;
}
public Boolean isLastQuestion() {
if(currentQuestionIndex == q.size() - 1){
return true;
}
else{
return false;
}
}
public Question getCurrentQuestion() {
return currentQuestion;
}
public void setCurrentQuestion(Question currentQuestion) {
this.currentQuestion = currentQuestion;
}
public int getScore() {
return score;
}
}
DrivingTestM.java (the test file):
import java.io.FileNotFoundException;
public class DrivingTestMain {
public static void main( String args[] ) throws FileNotFoundException
{
DrivingTest drivingTest = new DrivingTest();
while( true )
{
// display the current question
Question question = drivingTest.getCurrentQuestion();
System.out.println( question.getDescription() );
System.out.println( "\t" + question.getAnswerA() );
System.out.println( "\t" + question.getAnswerB() );
System.out.println( "\t" + question.getAnswerC() + "\n" );
// set the answer to the current question to 1
drivingTest.getCurrentQuestion().setAnswer( 1 );
// if this is the last question, we are done.
if( drivingTest.isLastQuestion() ) break;
// it is not the last question, so increment CurrentQuestionIndex
int currentQuestionIndex = drivingTest.getCurrentQuestionIndex();
drivingTest.setCurrentQuestionIndex( currentQuestionIndex + 1 );
}
// display the test score
System.out.println( "Your test score is: " + drivingTest.getScore() );
}
}
you're not setting the current question anywhere before you are trying to use it.
you need a method like:
public void startTest()
{
currentQuestion = q.get(0);
}
and then:
DrivingTest drivingTest = new DrivingTest();
drivingTest.startTest();
while( true )
{
//....
also ensure you have questions to get, or you will get other errors as well..
if you are using eclipse, try Debugging your code by stepping through it...
you might want to look in a better way to terminate that loop as well, currently it will fail at the end of the test...
EDIT: ok the loop wont break, but its really messy...
Edit of the DrivingTest constructor:
public DrivingTest() throws FileNotFoundException{
File f = new File("DrivingTest.txt");
//int n = 1;
Scanner sc = new Scanner(f);
while(sc.hasNextLine()){
String desc = sc.nextLine();
String A = sc.nextLine();
String B = sc.nextLine();
String C = sc.nextLine();
String h = sc.nextLine();
int a = Integer.parseInt(h);
String blank = sc.nextLine();
q.add(new Question(desc, A,B,C,a) );
}
sc.close();
//ensure it's 0.
currentQuestionIndex = 0;
//sets up your first question object.
setCurrentQuestion( q.get(currentQuestionIndex) );
}
You will get java.lang.NullPointerException because you store your Question object in Q list but you don't read objects from it. Try something like this in main class instead of:
Question question = drivingTest.getCurrentQuestion();
use
List<Question> qList = drivingTest.getQ();
Question question = qList.get(i);
also add Q getter in your DrivingTest class.
If you don't want modify main class than make changes in getCurrentQuestion() method:
int i = 0;
public Question getCurrentQuestion() {
if (i<q.size()-1)
return q.get(i++);
else return q.get(i);
}
.

Compilation error: type mismatch [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Changed my code since I should protect my code from students against plagiarism.
I got this error when I tried to compile my code:
Unresolved compilation problem:
Type mismatch: cannot convert from Object to Entry
at SchoolManager.listGrade(SchoolManager.java:49)
at SchoolManager.main(SchoolManager.java:30)
mainClass.java
public class mainClass {
static Scanner scanner = new Scanner(System.in);
static ArrayList<C> c = new ArrayList();
static ArrayList<Object> ob = new ArrayList();
public static void main(String [] args){
while(true){
System.out.println("----------------------------------------------------------------------------");
System.out.println("New:1 Add:2 ListC:3 ListOb:4 AddE:5 ListE:6");
System.out.println("----------------------------------------------------------------------------");
System.out.print(">> ");
String option = scanner.nextLine();
if(option.equals("0")){
System.out.println(c.size()+" "+ob.size()+" ");
System.out.println("Bye");
break;
}else if(option.equals("1")){
c.add(createC());
}else if(option.equals("2")){
ob.add(createOb());
}else if(option.equals("3")){
listC();
}else if(option.equals("4")){
listOb();
}else if(option.equals("5")){
addE();
}else if(option.equals("6")){
listE();
}else{
System.out.println(" ");
}
}
}
private static void listE() {
System.out.print("");
String ob1 = scanner.nextLine();
Object ob = findOb(ob1);
if(ob==null){
System.out.println(" ");
}else{
System.out.println(" ");
System.out.println(ob);
System.out.println(" ");
for(int i=0;i<ob.getE().size();i++){ // this part is a problem.
Entry e = ob.getE().get(i); // this part is a problem.
System.out.println(e.getC()+"\t"+ge.getT()+"\t"+ge.getE());
}
}
}
private static void addE() {
System.out.print(" ");
String ob1 = scanner.nextLine();
Object ob = findOb(ob1);
if(ob==null){
System.out.println("Could not be found...");
}else{
addE(ob);
}
}
private static Object findOb(String ob1) {
for(int i=0;i<ob.size();i++){
if(ob.get(i).getID().equals(ob1)){
return ob.get(i);
}
}
return null;
}
private static void addE(Object ob) {
System.out.print("? ");
String c_id = scanner.nextLine();
C c = findC(c_id);
if(c==null){
System.out.println("Could not be found...");
}else{
System.out.print("T? ");
String t = scanner.nextLine();
System.out.print("E? ");
String ee = scanner.nextLine();
ob.AddE(new Entry(c,t,ee));
}
}
private static C findC(String c_id) {
for(int i=0;i<c.size();i++){
if(c.get(i).getID().equals(c_id)) return c.get(i);
}
return null;
}
private static void listOb() {
for(int i=0;i<ob.size();i++){
System.out.println(ob.get(i));
}
System.out.println();
}
private static void listC() {
for(int i=0;i<c.size();i++){
System.out.println(c.get(i));
}
System.out.println();
}
private static Object createObject() {
System.out.print("S? ");
String id = scanner.nextLine();
System.out.print("N? ");
String n = scanner.nextLine();
System.out.print("S? ");
String s = scanner.nextLine();
return new Student(id,n,s);
}
private static C createC() {
System.out.print("C? ");
String id = scanner.nextLine();
System.out.print("Ct? ");
String Ct = scanner.nextLine();
return new Course(id,Ct);
}
}
Object.java:
public class Object {
public String ID;
public String n;
public String s;
public ArrayList<C> c;
public ArrayList<Ob> s;
public ArrayList<Entry> e;
public Object() {
}
public Object(String id, String n, String s) {
this.ID = id;
this.n = n;
this.s = s;
c= new ArrayList<C>();
ob= new ArrayList<Ob>();
e= new ArrayList<Entry>();
}
public String getId() {
return ID;
}
public String getN() {
return n;
}
public String getS() {
return s;
}
public String getID() {
return getId();
}
public void AddE(Entry e) {
e.add(Entry); //this part is a problem.
}
public ArrayList<Object> getE() {
return ob; //this part is a problem.
}
public String toString() {
String result = ID + " " + n + " " + s;
for(int i=0; i < c.size(); i++) {
result += c.get(i).toString();
result += "\n";
}
return result;
}
}
C.java:
import java.util.ArrayList;
public class C {
String co;
String t1;
public ArrayList<Ob> ob;
public C(String id, String t1) {
co = id;
this.t1 = t1;
ob = new ArrayList<Object>();
}
public String getCo() {
return co;
}
public String getT1() {
return t1;
}
public String getID() {
return getCo();
}
public String toString() {
String result = co + " " + t1;
for(int i=0; i < ob.size(); i++) {
result += ob.get(i).toString();
result += "\n";
}
return result;
}
}
Entry.java:
public class Entry {
public C c;
public String t;
public String g;
public ArrayList<Ob> g;
public Entry(C c, String term, String gr) {
c=c;
t=t;
gr=gr;
g = new ArrayList<Ob>();
}
public Course getC() {
return c;
}
public String getE() {
return g;
}
public String getT() {
return t;
}
public String toString() {
String result = c+ " " + t+ " " + g;
for(int i=0; i < gr.size(); i++) {
result += gr.get(i).toString();
result += "\n";
}
return result;
}
}
How can I solve this error?
This is the error it is talking about:
GradeEntry ge = student.getGrades().get(i);
Says can't convert from Student to GradeEntry, so I suspected getGrades was actually returning Students and yes that is the case:
public ArrayList<Student> getGrades() {
return students; //this part is a problem.
}
I think you meant to return grades:
public ArrayList<Grade> getGrades() {
return gr;
}

Categories