0

I'm a beginner in Java. I have an assignment that require me to take 3 input from user, then output the 3 at the same time. here is my code. i have only get 1 output. suppose look like this:

example anyone could help, thx!

here is my code

Scanner sc = new Scanner(System.in);

  int i = 0;
  String classname = " ";
  String rating = " ";
  int plus = 0;


  while(i < 3){
    System.out.print("What class are you rating? ");
    classname = sc.nextLine();

    System.out.print("How many plus signs does " + classname +" get? ");
    rating = sc.nextLine();
    plus = Integer.parseInt(rating);

    i++;
  }
    System.out.print(classname + ": ");

    while (plus > 0){
      System.out.print("+");
      plus --;
    }
    System.out.println();
2
  • Can we see your program from part B? And could you tell us if you know about arrays and Java Beans (specifically plain old java objects)? Commented Feb 17, 2020 at 7:24
  • You take 3 inputs but don't save the values. After you exit your loop, you're left with only the most recent classname & rating, and only display last set of values. If you want to dump all the values after you finish the main loop, as opposed to just printing the ratings as soon as you answer the question in each iteration, you have to store the results in an array or linked list, then create a loop after questions are answered (i.e. after you exit the loop) to retrieve and decode the values you stored in the array or list and display the name and ratings from that retrieved set of values. Commented Feb 17, 2020 at 15:07

2 Answers 2

1

The very first thing I would do is create a Course POJO (Plain Old Java Object). It should have two fields, name and rating. And I would implement the display logic with a toString in that Course POJO. Like,

public class Course {
    private String name;
    private int rating;

    public Course(String name, int rating) {
        this.name = name;
        this.rating = rating;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < rating; i++) {
            sb.append("+");
        }
        return String.format("%s: %s", name, sb);
    }
}

Then your main method simply involves filling a single array of three Course instances in one loop, and displaying them in a second loop. Like,

Scanner sc = new Scanner(System.in);
Course[] courses = new Course[3];
int i = 0;
while (i < courses.length) {
    System.out.print("What class are you rating? ");
    String className = sc.nextLine();
    System.out.printf("How many plus signs does %s get? ", className);
    int classRating = Integer.parseInt(sc.nextLine());
    courses[i] = new Course(className, classRating);
    i++;
}
i = 0;
while (i < courses.length) {
    System.out.println(courses[i]);
    i++;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Part B is actually unrelated as the code is complete different. i just got an advice from my TA that "rather than printing a "+" in each loop, why not try concatenating it to a string and print it after the while loop" do you have any idea what did him mean?
@DemonKevin See my toString method in this answer.
i saw it, however we haven't went through that method which means i cant really use it.
Well, that is what your TA meant by "concatenating it to a string and print it after the w̶h̶i̶l̶e̶ loop".
If you meant StringBuilder specifically, it's a more efficient way to write String sb = ""; for (int i = 0; i < rating; i++) { sb = sb + "+"; } (which is using a StringBuilder internally and creating new temporary strings on every iteration).
|
0

You overwrite your variables classname and rating in each loop. You need to store each iteration in a field of an array.

Scanner sc = new Scanner(System.in);

  int i = 0;
  String[] classname = new String[3]; //create array
  String rating = " "; //rating can be overwritten, it is not needed after the loop
  int[] plus = new int[3];


  while(i < 3){
    System.out.print("What class are you rating? ");
    classname[i] = sc.nextLine(); //name[index] to read/write fields of an array
                                  //index starts at 0 

    System.out.print("How many plus signs does " + classname +" get? ");
    rating = sc.nextLine();
    plus[i] = Integer.parseInt(rating);

    i++;
  }
for(i = 0;i<3;i++){ //iterate over all elements in the array
    System.out.print(classname[i] + ": ");

    while (plus[i] > 0){
      System.out.print("+");
      plus[i] --;
    }
    System.out.println();
}

1 Comment

this did solve my problem. but we haven't went over arrays yet, so i better not use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.