简体   繁体   中英

Java: How to output user input (multiple Strings/Ints) from another method

I'm brand new to Java, and also new to this site, so please go easy on me. :)

I'm attempting to write a program which will ask the user for several pieces of information. After gathering the information from the user, I then need to call another method to print the information back to the console screen.

The problem that I'm having is that my final method to reprint all of the information to the screen is a wreck, and I don't know where to start to fix it. I ran my code prior to writing and calling the final method (printToScreen), and the program worked as expected with no errors or anomalies. Code is below, and I really REALLY appreciate any assistance.

import java.util.*;

public class Program5 {

//Create constants
public static final int TOTAL_SEATS = 50;
public static Scanner console = new Scanner(System.in);

public static void main (String[] args) {

  //Create variables and objects
  String courseCode, courseName;
  int studentsReg;
  int openSeats;

  //Call method to print three lines of 55 asterisks to screen
  screenBreak();

  //Call method to prompt the user for input
  promptCodeName();

  //Call method to ask for pre-requisites
  getPrereqs();

  //Call method to ask how many students are currently registered
  numStudents();

  screenBreak();

  printToScreen();

}//Close the main method

public static void screenBreak() {
  for (int i = 1; i <= 3; i++) {
     for (int j = 1; j <= 55; j++) {
        System.out.print("*");
     } //Close inner for loop
     System.out.println();
  } //Close outer for loop
} //Close screenBreak method

public static void promptCodeName() {
  String courseCode, courseName;

  System.out.print("Please enter the course code: ");
  courseCode = console.nextLine();

  System.out.print("Please enter the course name: ");
  courseName = console.nextLine();
}//close promptCodeName method

public static void getPrereqs() { 
  int numPrereqs;
  String listPrereq;

  System.out.print("How many pre-requisites does the course have? ");
  numPrereqs = console.nextInt();

  console.nextLine();

  for (int i = 1; i <= numPrereqs; i++) {
     System.out.print("List Pre-requisite #" + i + "? ");
     listPrereq = console.nextLine();

  }//Close for loop
}//Close getPrereqs method

public static void numStudents() {
  int studentsReg;
  System.out.print("How many students are currently registered for this course? ");
  studentsReg = console.nextInt();
}//Close numStudents method

public static int calcAvail (int seatsTaken) {
  return (TOTAL_SEATS - seatsTaken);
}//Close calcAvail method

public static void printToScreen () {
  String courseCode = console.nextLine;
  String courseName = console.nextLine;
  numPrereqs = console.nextLine;
  int studentsReg = console.nextInt;

  String listPrereq = console.nextLine;

  System.out.println(courseCode + ": " + courseName);

  System.out.print("Pre-requisites: ");
  for (int i = 1; i <= numPrereqs; i++) {
     System.out.print(listPrereq);
  }//Close for loop
  System.out.println("Total number of seats = " + TOTAL_SEATS);
  System.out.println("Number of students currently registered = " + studentsReg);

  openSeats = calcAvail(studentsReg);

  System.out.println("Number of seats available = " + openSeats);

  if (openSeats >= 5) {
     System.out.println ("There are a number of seats available.");
  }//Close if loop
  else {
     if (openSeats <= 0) {
     System.out.println ("No seats remaining.");
     }//Close if loop
     else {
           System.out.println ("Seats are almost gone!");
     }//Close else

  }//Close printToScreen method
}//Close Program5 class

Your problem is becouse courseCode, courseName are local variables which means they are only available in this promptCodeName (for example ofc) method.

If You want to store informations from user in variables, u should create fields in Your class and store informations user in it.

So create fields at the beginning of class (eq private String courseCode;) and then, method should looks like that:

public static void promptCodeName() {
  String courseCode, courseName;

  System.out.print("Please enter the course code: ");
  courseCode = console.nextLine();
  this.courseCode = courseCode;

  System.out.print("Please enter the course name: ");
  courseName = console.nextLine();
  this.courseName = courseCode;
}

Read more about "this" word, i think it will let You understand this. :)

Don't forget about scope of your variables. For example in method promptCodeName() you declare local variables courseCode and courseName and assign them to input from console, but you never use this variables (their values). So you have to declare class variables (in the same way as TOTAL_SEATS and scanner) and assign respective values to them or use your local variables from main method, but in this case you have to send them to respective methods as method parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM