passing int's without knowing the value.

the way i learned it was you need to know the value so i dont know wha

Page 1 of 1

1 Replies - 591 Views - Last Post: 18 March 2009 - 12:47 PM Rate Topic: -----

#1 saulfenky2470  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 11-December 08

passing int's without knowing the value.

Posted 18 March 2009 - 09:58 AM

hey, im trying to pass an int to the main. problem is that i dont know the value of the int and they way i learned it was that you had to know the value of it.
example:
public class chapterfour {
int x;
int y;
int z;
int a;
int b;

	public void passer() {
		chapterfour add = new chapterfour();
		chapterfour square = new chapterfour();
		//send to additup
		 x = 2;
		 y = 5;
		add.AddItUp(x,y);
		//send to squareit
		 a = 3;
		square.SquareIt(a);
		
	}
	
	public void AddItUp(int x, int y) {
		//z is the total
		 z = x + y;
		 System.out.println("5 and 2 added up is " + z);
		 
		 
	
	}
	
	public void SquareIt(int a) {
		//b is the total
		 b = a * a;
		System.out.println("3 squared is " + b);
	}
	
}



public class chapterfourTestDrive {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		chapterfour add = new chapterfour();
		chapterfour square = new chapterfour();
		//adds
		add.AddItUp(2,5);//<--see i know the value of them.
		//squares
		square.SquareIt(3);
 
		
			
	}

}



what i want to do is pass the value of and int that i dont know the value of. look here is my code any help would be great. everything is in there own class cause i really like OO coding
GET USER INPUT CLASS

import java.io.*;

public class input {
	public String getUserInput(String prompt) {

		String inputLine = null;
		System.out.print(prompt + "  ");
		try {
			BufferedReader is = new BufferedReader(
			 new InputStreamReader(System.in));
			inputLine = is.readLine();
			if (inputLine.length() == 0 ) return null;
		} catch (IOException e) {
			System.out.println("IOException: " + e);
		}
		return inputLine.toLowerCase();
	}
}


SET IT UP CLASS
public class start {
	input get = new input();
	String userInput;
	
	public void setup() {
		
		
		userInput = get.getUserInput("Ask the 8ball anything:");
		
		
		
	}
}



GET THE ANSWERS CLASS
public class answers {
	int a;
	int a2;

	public void getAnswers() {


		String an1 = "i dont know";
		String an2 = "chuck norris";
		String[] answers = {an1, an2};
		
		int lenghtanswers = answers.length;
		
		int rand1 = (int) (Math.random() * lenghtanswers);
		
		String answer = answers[rand1];
		if (answer == an1) {
		System.out.println(answer); 
		a++;
		}else { 
			System.out.println(answer);
			a2++;
		}
	}
}


AND MAIN
public class testdrive {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int x = 0;
		while (x < 200) {
		start go = new start();
		go.setup();
		answers run = new answers();
		run.getAnswers();
		x++;}
		System.out.println(“You asked me “ + x + " questions. And i answered “
+ a + “ of them I don’t know. And I answered “ + a2 + “ of them chuck norris. Why are you asking me.”

		}
	}


you see i add to a and a2 ONLY if that answer was put out. so i dont know the value of them. and to get that little funny joke and the end i need the value of a and a2. How do i pass the value of a and a2 with out knowing what they are is the exsact question im asking.


EDIT: oh and i know that it can tell if its an1 or an2 cause i once putout ANSWER 1 if it was an1 and ANSWER 2 if it was an2 and it worked.

Is This A Good Question/Topic? 0
  • +

Replies To: passing int's without knowing the value.

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: passing int's without knowing the value.

Posted 18 March 2009 - 12:47 PM

ok.. so you need to get user input? Here is an example of getting the user's input for a sum:
package DICForum;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class InputAdd {
	public static void main(String[] args) {
		int a, b, answer;
		a = getInteger("Please input first number: ");
		b = getInteger("Please input second number: ");
		//make sure a is smaller than (or equal to) b if not
		// then we will swap the values!
		if (a > b) { // if a > b
			int temp = a; // then put the value of a in a temporary place
			a = b; // assign the value of b to a
			b = temp; // assign the value of a to b
		}
		answer = a + b;
		System.out.printf("The sum: %d + %d = %d", a, b, answer);
	}
	
	public static int getInteger(String prompt)  {
		PrintStream out = System.out;
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		boolean gotValue = false;
		int retValue = 0;
		do {
			out.print(prompt);
			try {
				String input = in.readLine();
				retValue = Integer.parseInt(input);
				gotValue = true; // will not reach here if there was an exception...
			} catch (NumberFormatException nfe) { //Thrown if the number is not in a valid format
				out.println("Please input a VALID number!");
			} catch (Exception e) {
				e.printStackTrace();
				//some unknown exception... we don't wish to continue
				throw new RuntimeException(e);
			}
		} while (!gotValue);
		return retValue; 
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1