0

I'm a beginner in Java and wanted to know how can I read a list of objects in Java. I have a problem that asks to implement a list of rational numbers and print them (and some other methods)

    import java.util.Scanner;

public class Rational {
    private int a;
    private int b;

    public Rational (int a, int b){
        this.a = a;
        this.b = b;
    }

    @Override
    public String toString() {
        return a + "/" + b;
    }

    public static void main(String []args) throws Exception {
        // Take input from the user in the form of 2 numbers
        Scanner scanner = new Scanner(System.in);

    int n = scanner.nextInt();
    Rational array[] = new Rational[n];

    for(int i=0;i<n;i++) {

        int num = scanner.nextInt();
        int deno = scanner.nextInt();

    // Create a rational obj with 2 args
    Rational array[i] = new Rational(num, deno);
        }
    }
}

So I've tried to read an array of objects: ex: n=4 then first 2 3 second 5 4 .....

I'm getting an error saying Type mismatch: cannot convert from Rational to Rational[]

1
  • this may be help you, also in your constructor you should assign values to this.a and this.b and then @override toString method and return (a+"/"+b) Commented Oct 14, 2014 at 18:20

2 Answers 2

1

Assuming you want to take input from the keyboard:

Scanner scanner = new Scanner(System.in);
List<Rational> rationals = new ArrayList<>(); // create a list
String line = scanner.nextLine(); // read list of numbers; e.g., "1/2, 3/4, 7/8"
for (String rational : line.split(",")) { // split string at each ","
    String[] parts = rational.split("/"); // split each of those at "/" character
    rationals.add(new Rational( // parse each half as int; create Rational; add to list
        Integer.parseInt(parts[0].trim()),
        Integer.parseInt(parts[1].trim())));
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use something like

public class Rational {
    private int a;
    private int b;

    public Rational (int a, int b){
        this.a = a;
        this.b = b;
    }

    @Override
    public toString() {
        return a + "/" + b;
    }

    public static void main(String []args) throws Exception {
        // Take input from the user in the form of 2 numbers
        Scanner scanner = new Scanner(System.in);

        int num = scanner.nextInt();
        int deno = scanner.nextInt();

        // Create a rational obj with 2 args
        Rational x = new Rational(num, deno);
        System.out.println(x);
    }
}

7 Comments

I see that I can read only one object. I've seen at Boann that he created an entire string with this objects and used "," to separate each rational number. Is there any other way to read a list of objects? Thank you! I like your code.
Basically you can take any input in the form of String and then manipulate the String to get individual objects.
@Stefan There's any number of formats you could allow the user to use to input the numbers. It's up to you to decide what you want.
@Boann Thanks for the answer. I'm trying to create an array of n objects then to read each one separately. However I got a lot of errors and I don't know how to ignore "/". Your code looks very good but I'm having a hard time trying to figure how List<Rational> rationals = new ArrayList<>(); creates a list and some other instructions.
Use "//" this will help ... Since '/' is a special character you need to give one extra to ignore it
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.