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[]

Stefan
  • 83
  • 1
  • 6
  • 20
  • [this](http://stackoverflow.com/questions/17538182/getting-keyboard-input/17538216#17538216) 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)` – nachokk Oct 14 '14 at 18:20

2 Answers2

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())));
}
Boann
  • 48,794
  • 16
  • 117
  • 146
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);
    }
}
Parth Satra
  • 513
  • 2
  • 16
  • 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. – Stefan Oct 14 '14 at 18:45
  • 1
    Basically you can take any input in the form of String and then manipulate the String to get individual objects. – Parth Satra Oct 14 '14 at 18:50
  • @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 Oct 14 '14 at 18:57
  • @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 rationals = new ArrayList<>(); creates a list and some other instructions. – Stefan Oct 14 '14 at 19:43
  • Use "//" this will help ... Since '/' is a special character you need to give one extra to ignore it – Parth Satra Oct 14 '14 at 19:46
  • @ParthSatra There is nothing special about '/'. You're thinking of '\'. – Boann Oct 14 '14 at 19:57
  • I've tried to ignore that for the moment and simply read an array of objects, however I get the following error: Type mismatch: cannot convert from Rational to Rational[] – Stefan Oct 14 '14 at 20:24