SRL Archive - Java Help and Tutorials

Attempting java


All that is man

2009-10-25 21:51:07

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

2009-10-25 21:57:06

if(input != "n" || input != "y"){
Should be
if(input != "n" && input != "y"){

All that is man

2009-10-25 21:59:26

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

2009-10-25 22:00:55

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

2009-10-25 22:02:43

Whats the equals method in the String class?

mrpickle

2009-10-25 22:20:49

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

2009-10-25 23:00:03

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

2009-10-25 23:11:01

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

2009-10-26 11:35:53

Java doesn't support switches with any other data type except numbers.

Baked0420

2009-10-26 13:05:42

I think what he wants, because he said it isn't reading it, is this:


[Java] lol
Scanner in = new Scanner(System.in);

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

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

2009-10-26 15:29:13

From: ape
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

2009-10-30 04:37:17

I'm confused, I've never encountered a "bananas" class…

From: Nadeem
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

2009-10-30 07:36:09

And afaik you should always put break after each case thing, soo

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

Freddy1990

2009-10-30 14:15:08

From: noidea
And afaik you should always put break after each case thing, soo

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


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

Sir R. M8gic1an

2009-11-01 10:22:27



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

2009-11-01 10:56:11

From: Freddy1990
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