How do I perform two tests with different test values?

1.7k Views Asked by At

I am a college student and this is my first semester learning Java programming. For the past few days I've been stuck on some things when learning Java. One activity I'm stuck on for my class is this one:

Retype the statements, correcting the syntax errors.

System.out.println("Num: " + songnum);
System.out.println(int songNum);
System.out.println(songNum " songs");

Note: These activities may test code with different test values. This activity will perform two tests: the first with songNum = 5, the second with songNum = 9.

This is what I have so far:

import java.util.Scanner;

public class Errors {
   public static void main (String [] args) {
      int songNum;

      songNum = 5;

      System.out.println("Num: " + songNum);
      System.out.println("5");
      System.out.println("5 " + "songs");

The website we are using called Zybooks says the code above is correct for outputting:

Num: 5

5

5 songs

But I can't figure out what to do to output the same but with the number 9. I've tried doing the same 3 lines for it but it says it's not the correct way to do it. How do output both 5 and 9 for the values?

2

There are 2 best solutions below

0
WJS On

Change the 2nd and 3rd print statements to the following:

System.out.println(songNum);
System.out.println(songNum + " songs");

The problem was you hardcoded the value of 5 within a string making it impossible to change.

3
Bloody Assault On

You cannot print the number nine with the code that you wrote. if you want to get the number nine then you have to change the code and create a new variable e.g. x = 9; What you can do something like that:

import java.util.Scanner;

public class Errors {
   public static void main (String [] args) {
      int songNum;

      songNum = 5;
      x = 9;

      System.out.println("Num: " + songNum);
      System.out.println(songNum);
      System.out.println(songNum + " songs");

      System.out.println("Num: " + x);
      System.out.println(x);
      System.out.println(x+ " songs");

Your main issue is that you had cached your value (5) into a string, rendering it unchangeable.