Home » Java » while loop with store value for next while loop in java-Exceptionshub

while loop with store value for next while loop in java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

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();
How to&Answers:

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++;
}

Answer:

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();
}