PDA

View Full Version : Attempting java



All that is man
10-26-2009, 01:51 AM
import java.util.Scanner;
class test {
public static void main(String args[]){
boolean correct;

bananas lol = new bananas();
correct = lol.yesorno("Is your name Brian?");
if(correct == true){
System.out.println("Hello Brian!");
}else{
System.out.println("Then what is your name nub?!");
}
}
}



import java.util.Scanner;
public class testtwo {
public boolean yesorno(String question){
Scanner readInput = new Scanner(System.in);
int tempInt = 1;
boolean tempBool = false;
String input;
System.out.println(question + " <y / n>");
do{
input = readInput.nextLine();
if(input == "y"){
tempInt = 2;
tempBool = true;
}
if(input == "n"){
tempInt = 2;
tempBool = false;
}
if(input != "n" || input != "y"){
System.out.println("Please enter y or n!");
}
}while(tempInt != 2);
return tempBool;
}
}


But no matter what I input it doesn't read it :/


Is your name Brian? <y / n>
y
Please enter y or n!
n
Please enter y or n!


any help?

bullzeye95
10-26-2009, 01:57 AM
if(input != "n" || input != "y"){
Should be
if(input != "n" && input != "y"){

All that is man
10-26-2009, 01:59 AM
Is your name Brian? <y / n>
y
Please enter y or n!
n
Please enter y or n!
maybe
Please enter y or n!


Still didn't solve my problem :/ anything else to try?

Method
10-26-2009, 02:00 AM
To compare Strings, you need to use the equals method in the String class, not the == operator. There's a big difference between the two.

All that is man
10-26-2009, 02:02 AM
Whats the equals method in the String class?

mrpickle
10-26-2009, 02:20 AM
I'm also learning Java atm. I think after getting hang of a little bit of delphi everything just kinda falls itself into place.

Equals method in the string class?

you mean like:

If ('stringVar1' = 'stringVar2'){
//Stuff
}

?
dont forget to convert variable so they match when you are using the if statements!

Here's my assignment programs (yeah for school, take it as a helper if you need :)) - Done with Ready to Program.


import java.applet.*; // some of the nessisary inports
import java.awt.*;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class FedTax
{
public static void EndingComment (String name, double salary, double tax, double result) throws IOException
{ // This is my method, used to shorten the code & for learning.
// Has all the final output statements bundled together
System.out.println ("");
System.out.println (name + ", your annual salary will have a federal tax rate of " + (tax * 100) + "%");// tax rate output
System.out.println (name + ", you will be taxed " + result + " dollars for your annual salary");// amount deducted from income
System.out.println (name + ", your annual salary after tax will be " + (salary - result)); // salary after tax
}


public static void main (String[] args) throws IOException // main method
{
// initializes the buffered reader for keyboard input detection
InputStreamReader inStream;
BufferedReader keyboard;
inStream = new InputStreamReader (System.in);
keyboard = new BufferedReader (inStream);

//My variables
String sResult, name, sSalary;
double tax, result, salary;

// The main menu for the user with al the rates & basic calculating info
System.out.println ("The Calculator For Federal Tax");
System.out.println ("-----------------------------");
System.out.println ("---The Tax Reference Chart---");
System.out.println ("-----------------------------");
System.out.println ("Annual Salary Federal Tax Rate");
System.out.println ("$0 - $10 000 0%");
System.out.println ("$10 001 - $25 000 20%");
System.out.println ("$25 001 - $50 000 25%");
System.out.println ("$50 001 - $100 000 25%");
System.out.println ("$100 000 + 35%");
System.out.println ("------------------------------");

System.out.println ("Ur name plz");// asks & gets name
name = keyboard.readLine ();
System.out.println ("Ur annual salary plz (If you do words, i'm not calculating for u)");// asks and gets salary
sSalary = keyboard.readLine ();

salary = Double.parseDouble (sSalary);

if (salary > 100000)// if salary is more than 100000
{
tax = 0.35; //35% tax
result = ((salary - 100000) * tax - 3000 - 6250 - 9000);// calculates amount of tax paid
EndingComment (name, salary, tax, result);
}
else if (salary < 10001)// if salary is less than 10000
{
tax = 0;// no tax
result = 0;// no calculation cuz no tax
EndingComment (name, salary, tax, result);
System.exit (0);
}
else if (salary > 10000)
{
if (salary < 25001)// if salary is more than 10000 & less than 25001
{
tax = 0.20;// 20% tax
result = ((salary - 15000) * tax);// calculates amount of tax paid
EndingComment (name, salary, tax, result);
}
}
else if (salary > 25000)
{
if (salary < 50001)// if salary is more than 25000 & less than 50001
{
tax = 0.25;// 25% tax
result = ((salary - 25000) * tax - 3000);// calculates amount of tax paid
EndingComment (name, salary, tax, result);
}
}
else if (salary > 50000)
{
if (salary < 100001)// if salary is more than 50000 & less than 100000
{
tax = 0.3;// 30% tax
result = ((salary - 50000) * tax - 3000 - 6250);// calculates amount of tax paid
EndingComment (name, salary, tax, result);
}
}
}
}

ape
10-26-2009, 03:00 AM
What method means is this...

string1.equalsIgnoreCase(string2)
You can make it just equals, but the ignore casing is useful in your situation.

Java does things slightly different than delphi. You would not use single quotes '
as this is used for defining char (character) values. Use the double quotes " instead as these define String values.

Only use > = < signs for direct comparison. such as...

if(2 >= 1) {

if("a" == "a") {etc...

For objects, you need to use the obj1.equals(obj2). String happens to be an object and unless you compare that same variable against itself, it wont return true. Thus the need for the obj1.equals(obj2). This then compares the values of one char[] (char array) against the other char[] giving you the result. Its like comparing a can of peaches to another can of peaches. That one can of peaches is not the same as the other can of peaches... but if you use the equals() it will compare the main contents, and since they both contain peaches, it will return true.

Nadeem
10-26-2009, 03:11 AM
You could also do something like:

do {
input = readInput.nextLine();
switch (input.toLowerCase()) {
case 'y': return true;
case 'n': return false;
default: System.out.println("Please enter 'y' or 'n'!");
}
} while(true);

Now you won't have to use those extra temp variables either (:



~NS

ape
10-26-2009, 03:35 PM
Java doesn't support switches with any other data type except numbers.

Baked0420
10-26-2009, 05:05 PM
I think what he wants, because he said it isn't reading it, is this:



lol
Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");
String number = in.nextLine();



EDIT: you also have in.nextInt and in.nextDouble and I don't know bout this one, but probably in.nextBoolean, but not sure bout that, but it'd make sense if it existed.


and what I showed you, I'm almost positive it's what your missing, since you include the scanner class, but never use it. ;)

EDIT: that's with the first one, which is also the one that asks about being brian or not, the second one you did use the scanner class, you just forgot to the first time :p

Nadeem
10-26-2009, 07:29 PM
Java doesn't support switches with any other data type except numbers.

Your wrong. Java supports char (: I just forgot to add .charAt(0). So fixed it would be:


do {
input = readInput.nextLine();
switch (input.toLowerCase().charAt(0)) {
case 'y': return true;
case 'n': return false;
default: System.out.println("Please enter 'y' or 'n'!");
}
} while(true);




~NS

Freddy1990
10-30-2009, 08:37 AM
I'm confused, I've never encountered a "bananas" class...


Your wrong. Java supports char (: I just forgot to add .charAt(0). So fixed it would be:


do {
input = readInput.nextLine();
switch (input.toLowerCase().charAt(0)) {
case 'y': return true;
case 'n': return false;
default: System.out.println("Please enter 'y' or 'n'!");
}
} while(true);




~NS
Hmm, I'm afraid you failed to make a point there :p Characters are just byte values and in this case also handled as values :)

noidea
10-30-2009, 11:36 AM
And afaik you should always put break after each case thing, soo


switch(x) {
case 1: System.out.println("Ohai, x = " + x); break;
case 2: System.out.println(":O x = "+ x); break;
}

Freddy1990
10-30-2009, 06:15 PM
And afaik you should always put break after each case thing, soo


switch(x) {
case 1: System.out.println("Ohai, x = " + x); break;
case 2: System.out.println(":O x = "+ x); break;
}

Good point, lol, didn't even notice that :p

Sir R. M8gic1an
11-01-2009, 03:22 PM
char yesOrNo;
Scanner in = new Scanner(System.in);
yesOrNo = scan.next().charAt(0);

switch (yesOrNo) {
case 'y':
case 'Y': System.out.println("yes"); break;
case 'n':
case 'N': System.out.println("no"); break;
default : System.out.println("wrong input."); break;
}


~RM

Nadeem
11-01-2009, 03:56 PM
I'm confused, I've never encountered a "bananas" class...


Hmm, I'm afraid you failed to make a point there :p Characters are just byte values and in this case also handled as values :)
I didn't use Characters in mine :p I use char, they are two different identifiers in java ;) And yes they are byte values:thumbsup: and don't need break; in the snippet i gave because return will automatically break it :)



~NS