0

For my homework problem, we enter number of students, then their names and numeric grades (0 - 100)

My problem is, I have to use a for loop for the number of students to enter their name and grade together. But if I do this I won't be able to separately print each name and grade.

I have a for loop that goes until the previously inputted number of students is reached, but this will not allow me to print all of the names and grades together at the end. The loop will ask for the students name and input, then the numeric grade and input. I cannot use arrays because we have not gotten to that point in our class so I will receive a 0 if I use them. I cannot use separate variables for each name and grade because the number of students is not a constant.

//loop input for all student names and their numeric grades
for(cntr = 0; cntr < numStudents; cntr++) { 
    System.out.print("Enter student name: ");
    studentName = input.nextLine();

    System.out.print("Enter " + studentName +"'s numeric grade: ");
    numGrade = input.nextInt();
    cleanUpStr = input.nextLine();
    }

At the end I should be able to print each name followed by their grade.

Example:

Bob 50

bill 60

dill 90

5
  • You're looking for a List (docs.oracle.com/javase/8/docs/api/java/util/List.html) or even an Array in your case. Just store the Input into the collection and iterate over it at the end. Commented Nov 11, 2019 at 20:54
  • I think you need to use indexes but if you don't share the data it will be hard to help. If you cannot use arrays where is the data stored? In Strings? Commented Nov 11, 2019 at 20:54
  • Unfortunately I can't use arrays for this, my prof wont let us use things we haven't learned yet in class. Commented Nov 11, 2019 at 20:58
  • numGrade is an integer, and studentName is a string. Commented Nov 11, 2019 at 20:59
  • Why not just add a second for-loop to print? Declare an output string to append each student and grade to as they're entered, and then when you exit the loop, just print that string? Commented Nov 11, 2019 at 21:10

3 Answers 3

1

Since you cannot use Array, List, or StringBuilder, you can simply append the typed in values to a String along with \n or a line terminator to move to the next line:

String names= "";
for(int cntr = 0; cntr < numStudents; cntr++) { 
    System.out.print("Enter student name: ");
    studentName = input.nextLine();

    System.out.print("Enter " + studentName +"'s numeric grade: ");
    numGrade = input.nextInt();
    cleanUpStr = input.nextLine();
    names += studentName + " " + numGrade + "\n";
}
System.out.println(names);

Note I added a String names which appends studentName and numGrade each loop iteration.

Obviously there are better solutions if you were not constrained by the professor.

Example Run:

Enter student name: bob
Enter bob's numeric grade: 50
Enter student name: bill
Enter bill's numeric grade: 60
Enter student name: dill
Enter dill's numeric grade: 90
Enter student name: hill
Enter hill's numeric grade: 20
Enter student name: mill
Enter mill's numeric grade: 40
bob 50
bill 60
dill 90
hill 20
mill 40
Sign up to request clarification or add additional context in comments.

Comments

0

You say that you can't use things you've not learned in class yet, which includes arrays, Lists, and other data types. So, logically, you have to think about other ways to collect the information such that you can print it at the end.

Since you're using Strings in your existing code, I will assume you have learned about Strings and are allowed to use them in your assignment. Since you print a String, then the solution would be to just keep collecting the student names and grades in a single stream as the user inputs it, and then, once you exit the for-loop, print it.

Now, the correct way to do this would be to use a StringBuilder. I personally never learned about StringBuilder in school so I think it unlikely it's on your list of things you are allowed to use, so this example will instead just concatenate (eg combine two strings).

First, you need to declare a String that will hold your output.

String output = "";

Notice it is initialized to an empty string, not null. This is because when you add two strings, if one is null then you'll get the word "null" randomly in your output.

The other important thing is that you have to declare this variable before the for-loop. Recall the scope of variables: anything you declare inside the for-loop is only available to you while you're inside of the for-loop; if you don't declare it outside of the loop, you won't be able to do concatenation at all.

Then in your for-loop, append the values to the string:

//loop input for all student names and their numeric grades
for(cntr = 0; cntr < numStudents; cntr++) { 
  studentName = input.nextLine(); 
  // snip
  numGrade = input.nextInt();
  output += studentName + " " + numGrade + "\n";
}

(The \n is a newline, which will allow us to print each student's name and grade on a separate line.)

And then finally, outside of the for-loop, print the results:

System.out.println(output);

Now, for completeness' sake, the correct way of appending to a String within a loop is to use a StringBuilder:

StringBuilder sb = new StringBuilder();

for(cntr = 0; cntr < numStudents; cntr++) { 
  // snip
    studentName = input.nextLine();
  // snip
    numGrade = input.nextInt();
    sb.append(studentName).append(" ").append(String.valueOf(studentGrade)).append("\n");
  // snip
}

System.out.println(sb.toString());

Comments

0

@Nexevis is correct. Here is a something that should be easy for a student to understand:

    String studentsRecords = "";

    int numStudents = 5;

    int studentGrade;

    String studentName;

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

        System.out.print("Enter student name: ");
        studentName = input.nextLine();
        studentsRecords = studentsRecords + "\n" + studentName;

        System.out.print("Enter " + studentName +"'s numeric grade: ");
        studentGrade = input.nextInt();
        studentsRecords = studentsRecords + " " + studentGrade;

    }


    System.out.println(studentsRecords);

Use concatenation and the newline \n character

Comments

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.