# passing int's without knowing the value.

Page 1 of 1

## 1 Replies - 618 Views - Last Post: 18 March 2009 - 12:47 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=93710&amp;s=b43377475c8f89642e35b81ad99b650a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 saulfenky2470

Reputation: 0
• 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 square = new chapterfour();
x = 2;
y = 5;
//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 square = new chapterfour();
//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 {
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:");

}
}

int a;
int a2;

String an1 = "i dont know";
String an2 = "chuck norris";

int rand1 = (int) (Math.random() * lenghtanswers);

a++;
}else {
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();
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

Reputation: 2252
• 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.PrintStream;

public static void main(String[] args) {
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
}
System.out.printf("The sum: %d + %d = %d", a, b, answer);
}

public static int getInteger(String prompt)  {
PrintStream out = System.out;
boolean gotValue = false;
int retValue = 0;
do {
out.print(prompt);
try {
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
} catch (Exception e) {
e.printStackTrace();
//some unknown exception... we don't wish to continue
throw new RuntimeException(e);
}
} while (!gotValue);
return retValue;
}
}