Jump to content

I'm currently working on an assignment, and the part I'm working on right now is the second method which should be:"The second method will be used to ask for the number of pre-requisites and perform a cumulative algorithm to create a list of pre-requisites.". I will link my program below, I have no idea why it won't run! I will link my program, aswell as the outlines for the entire assignment. 

Program #4 Description.pdf

Paluso4.java

Link to comment
https://linustechtips.com/topic/903398-java-programming-class-help/
Share on other sites

Link to post
Share on other sites

You should always post your code in the post or if it's a large project to some g it repo

 


import java.util.*;public class Paluso4 {public static final int SEATS = 50;   public static void main (String[] args) {   line(3);   String Coursestring = console1();   prereqs = console2();   }      //start loop   public static void line (int times){for(int n = 1; n <=times; n++) {      for (int t = 0; t <=54;t++) {            System.out.print("*");      }      System.out.println();      }      }    //end loop        //start console 1public static String console1 () { //variables      String Coursecode, Coursename;Scanner console = new Scanner(System.in);System.out.println("Please enter the course code: ");  Coursecode = console.nextLine();    System.out.println("Please enter the course name: ");  Coursename = console.nextLine();  String Coursestring = Coursecode + " " + Coursename;  return Coursestring;}//end console 1   //start console 2   public static void console2 (){   Int numprereq=0; String prereqcode;   System.out.println("How many pre-requisites does the course have?");numprereq = console.nextInt();for (int i = 1; i <= numprereq; i++) {   System.out.print("List pre-requisite #" + i);   String prereqcode = console.nextLine();   prereqs = prereqcode;   }   return prereqs;   }   }
	

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

  • 2 weeks later...
public static void console2 ()
{
    int numprereq=0; 
  	String prereqcode;

    System.out.println("How many pre-requisites does the course have?");
    numprereq = console.nextInt();
    for (int i = 1; i <= numprereq; i++) {
      System.out.print("List pre-requisite #" + i);
      String prereqcode = console.nextLine();
      prereqs = prereqcode;
    }
  
    return prereqs;
}

I'm not particularly good at Java, but I think the problem lies in the "prereqs = prereqcode;" portion. The for loop keeps overwriting prereq, so only the last value is returned.

 

You could maybe use a StringBuilder object or something to append stuff to the string as it's being built. Then you can return builder.toString().

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×