So my code is suppose to get information from the user and figure out how much funding the two professors get. My code runs twice but for some reason only the last entry is saved and outputted and my formula for calculating the new funding only outputs zero for some reason here is a sample of an output.
Name: Steve
Position: Assitant Professor
Institute: UCLA
Number of Papers Published: 11
Past Funding: $200,001.00
75
0.0

Heres the two classes

import javax.swing.JOptionPane;


public class NSFFunding {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scientist scientist = new Scientist();
		scientist.createScientist();
		
		System.out.print(scientist.createScientist());		
		System.out.println(scientist.getScore());
		
		double funding;
		int score = scientist.getScore();
		funding = (score/100 * 500000);
		System.out.print(funding);
	}

}
import java.text.NumberFormat;

import javax.swing.JOptionPane;


public class Scientist {

	/**
	 * @param args
	 */
	
	
	public int score = 0;
	public String createScientist() {
		String output = "";
		String name1 = JOptionPane.showInputDialog(null, "Please enter your name: ", 
	    		"Lab 3", JOptionPane.QUESTION_MESSAGE);
		output += ("Name: " + name1 + "\n");
		String institute1 = JOptionPane.showInputDialog(null, "Enter your institute name: ", 
	    		"Lab 3", JOptionPane.QUESTION_MESSAGE);
		String research1 = JOptionPane.showInputDialog(null, "Enter your research topic: ", 
	    		"Lab 3", JOptionPane.QUESTION_MESSAGE);
	    int response = Integer.parseInt(JOptionPane.showInputDialog(null, "1) Full Professor \n2) Associate Professor " +
	    		"\n3) Assistant Professor \n4) Others", 
	    		"Lab 3", JOptionPane.QUESTION_MESSAGE));{
			
			switch (response) {
			case 1:
			{
				score = score + 30;
				output +=("Position: Full Professor\n");
			}
			break;
			case 2:
			{
				score = score + 20;
				output +=("Position: Associate Professor\n");
			}
			break;
			case 3:
			{
				score = score + 10;
				output +=("Position: Assitant Professor\n");
			}
			break;
			case 4:
			{
				score = score + 5;
				output += ("Position: Other\n");
			}
			break;
			default:
				JOptionPane.showMessageDialog(null, "Sorry! No such option is on the menu. " +
	        			"Please eneter it again", 
	        			"Lab 3", JOptionPane.WARNING_MESSAGE);
	            break;
			}
		
		}
	    		output += ("Institute: " + institute1 + "\n");
	    
	    
		int papersPublished = Integer.parseInt(JOptionPane.showInputDialog(null, "Number of Papers Published: ", 
	    		"Lab 3", JOptionPane.QUESTION_MESSAGE)); {
	    			output += ("Number of Papers Published: " + papersPublished + "\n");
			if(papersPublished >= 30)
			{
				score = score + 30;
			}
			if( 20 <= papersPublished )
			{
				score = score + 20;
			}
			if(10 <=papersPublished)
			{
				score = score + 10;
			}
			if(papersPublished < 10)
			{
				score = score + 5;
			}
			
		}
		String userPastFunding = (JOptionPane.showInputDialog(null, "Past Funding Awarded: ", 
	    		"Lab 3", JOptionPane.QUESTION_MESSAGE));
		int pastFunding = Integer.parseInt(userPastFunding);
		{
			
			if(pastFunding >= 400000)
			{
				score = score + 40;
			}
			if(300000 <= pastFunding)
			{
				score = score + 30;
			}
			if(200000 <= pastFunding)
			{
				score = score + 20;
			}
			if(pastFunding < 200000)
			{
				score = score + 10;
			}
	    		}
		NumberFormat currency = NumberFormat.getCurrencyInstance( ); 
		userPastFunding = currency.format(pastFunding);
		output += ("Past Funding: " + userPastFunding + "\n");
		return output;
	}
	public int getScore(){
		return score;
	}

}

what makes you think your code runs twice?
can you give an example of the input you've given, and the output you actually expect for that input?

clumsy me, missed the obvious.
your code runs twice, because you call your createScientist() method twice, here:

scientist.createScientist();
 
System.out.print(scientist.createScientist());

and you only print it once, so the second time, you overwrite any values you've found during the first run.

Edited 4 Years Ago by stultuske: n/a

also just spotted your second problem. take a look at this (slightly altered version of) your main method:

public static void main(String[] args) {
		Scientist scientist = new Scientist();
			
		System.out.print(scientist.createScientist());
		System.out.println(scientist.getScore());
                // above, use the two basic methods. to get the funding, is below
		
		double funding;
		int score = scientist.getScore();
                // you originally started your calculations from an int, starting with
                // score / 100. but an int doesn't allow decimals, so in a lot of
                // cases, unless score is above 99, your score would be 0  before you
                // would multiply it with 500000, and 0 * 500000 = 0
                // you'll need to put it into the double first, or start with
                // multiplying
                funding = score;
                funding  = (funding/100) * 500000;
		System.out.print(funding);
	}

Thanks that made the code work perfectly, did you know why the code was only printing out the very last scientist's information because i am suppose to out print two scientist information and compare them.

as I said, your createScientist method returns a String that prints all that info (not really the way I would implement it, but it works) but you only printed the second one on the screen.

so: instead of
<your original main:>

public static void main(String[] args) {
Scientist scientist = new Scientist();
scientist.createScientist(); // this one won't be printed, since you don't pass it 
// to an actually printing method like System.out.print
 
System.out.print(scientist.createScientist());
System.out.println(scientist.getScore());
 
double funding;
int score = scientist.getScore();
funding = (score/100 * 500000);
System.out.print(funding);
}

try this:
<Re adapting my main:>

public static void main(String[] args) {
    Scientist scientist;
    for ( int i = 0; i < 2; i++ ){
         scientist = new Scientist();
     
        System.out.print(scientist.createScientist());
        System.out.println(scientist.getScore());
     
        int score = scientist.getScore();
        double funding = score;
        funding = (funding/100) * 500000;
        System.out.print(funding);
    }
}
This article has been dead for over six months. Start a new discussion instead.