-1

when compiling the program, an error pops up ("variable name, age, weight, height might not have been initialized) How to initialize correctly?

the program should output the values ​​entered above in a separate case

thank you in advance

package com.company;

import java.util.Scanner; 

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in); 
        boolean mainLoop=true;
        while(true) {
            System.out.println("MENU:");
            System.out.println("1-Name"+"\n"+"2-Age"+"\n"+"3-Height"+"\n"+"4-Weight"+"\n"+"5-print"+"\n"+"0-Exit"+"\n");
            System.out.print("Choose from 1 to 5:");
            int s = in.nextInt();
            switch (s) { 
                case 1:
                    System.out.print("1.Enter name: ");
                    String name = in.nextLine();
                    break;
                case 2:
                    System.out.print("2.Enter age: ");
                    int age = in.nextInt();
                    break;
                case 3:
                    System.out.print("3.Enter height: ");
                    int height = in.nextInt();
                    break;
                case 4:
                    System.out.print("4.Enter weight: ");
                    int weight = in.nextInt();
                    break;
                case 5:
                    System.out.println("Name:  %s" + name );
                    System.out.println("Age:   %d" + age);
                    System.out.println("Height:%d" + height + "cm" );
                    System.out.println("Weight:%d"  + weight + "kg" );
                case 0:
                    System.out.print("0.Exit");
                    return;
                default:
                    System.out.print("Wrong entry!");
                    break;
            }
        }
    }
}
Dnnn
  • 11
  • 1
  • You need to declare the variables at the start of your `main` function, initialising each with a default value (whatever you want to see if the user chooses 5 before they have chosen one of the other values). Then in your switch cases just do an assignment to the variable. – tgdavies Mar 31 '22 at 11:41
  • 1
    Or well, declare the variables within that block that needs them. Which would be: within the while loop. – GhostCat Mar 31 '22 at 11:42
  • Also note: your case 5 lacks a break ;-) – GhostCat Mar 31 '22 at 11:43
  • They need to be declaredoutside the while loop, @GhostCat, otherwise they lose their values each iteration. – tgdavies Mar 31 '22 at 11:57
  • True. But then: actually, you would want to reset them, at some point. One would rather not use a switch in the first place. But one after the other ask for the needed values, and then when they are all there print them. – GhostCat Mar 31 '22 at 12:06

1 Answers1

0

In case 5, you are trying to access variables , name, age, height and weight. What if user doesn't enter the the values of s in the given order and directly enters 5 ?. Thus the compiler gives an error when we are trying to access the variables without declaring them properly.

To overcome this error, you can either give default values for these variables, (say name ="", age = 0, height = 0, weight = 0) before entering the while loop so that the variables have default values.

Gurkirat Singh Guliani
  • 1,009
  • 1
  • 4
  • 19