Java ArrayIndexOutOfBoundsException when reading user input [duplicate] - java

This question already has answers here:
How can I avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException? [duplicate]
(2 answers)
Closed 7 years ago.
Whenever I try to use anything higher than eq[0], I end up with an ArrayIndexOutOfBoundsException.
My code:
import java.util.Scanner;
public class Calc{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String[] eq=in.next().split(" ");
double a=Double.parseDouble(eq[0]);
double b=Double.parseDouble(eq[-1]);
if(eq[1]=="+"){
System.out.println(">>"+String.valueOf(a+b));
}else if(eq[1]=="-"){
System.out.println(">>"+String.valueOf(a-b));
}else if(eq[1]=="/"){
System.out.println(">>"+String.valueOf(a/b));
}else if(eq[1]=="*"){
System.out.println(">>"+String.valueOf(a*b));
}
}
}

eq[-1] -1 index is problem
eq[1]=="+" // logical error use equals() method to compare String.
Scanner in=new Scanner(System.in);
String[] eq=in.nextLine().split(" "); // use nextLine() instead of next().
double a=Double.parseDouble(eq[0]);// 1st operand
double b=Double.parseDouble(eq[2]);// 3rd operand
if(eq[1].equals("+")){ //operator // compare string with equals method not with (==).
System.out.println(">>"+String.valueOf(a+b));
}else if(eq[1].equals("-")){
System.out.println(">>"+String.valueOf(a-b));
}else if(eq[1].equals("/")){
System.out.println(">>"+String.valueOf(a/b));
}else if(eq[1].equals("*")){
System.out.println(">>"+String.valueOf(a*b));
}

Firstly, eq[-1] the index should not be valid. I think it is only valid for Python.
Secondly, when you want to compare 2 strings, you should not use ==.
.equals function will help.

Related

Java - IF condition not reading entered String variable value [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String play = "y";
System.out.print("Enter something: ");
play = scan.next();
System.out.println(play);
if (play == "Y" || play == "y")
{
System.out.println("If test works!!");
}
System.out.println("Did it work???");
}
}
I assume this has something to do with when I press enter, it's storing that as well. I tried changing String play to a char, but then I get errors from Scanner saying it can't change a String to a char.
You should atmost avoid using “==“ when comparing objects especially strings. “==“ checks for object references. Change the comparison to use .equals method and it should work
if(play.equals(“Y”) || play.equals(“y”))
in case if “play” can be null, the below snippet is more safe.
if(“Y”.equals(play) || y.equals(play))

Comparing user input values [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I am not sure why but when I get a string from the user, I cannot compare it in an if statement but when I try to print it, it works fine.
Part of my code:
Scanner in = new Scanner(System.in);
while (true) {
String userInput;
int rowInput, colInput;
printBoard(board);
System.out.print("Move: ");
userInput = in.next();
// shift board right on a row
if (userInput == "r") {
System.out.print("row #: \r");
rowInput = in.nextInt();
moveRight(--rowInput, board);
}
Does anyone know why this isn't working as expected?
You an try this:
if (userInput.equals("r"))
== is used to compare the address and equals is used to compare contents.
I should be using equals instead of ==.
So it would lead to:
...
if (userInput.equals("r"))
...

nextLine() in java cannot get the exactly value I want [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
public static void main(String args[]){
System.out.print("Type new, wil display TRUE:");
Scanner sc = new Scanner(System.in);
String userInput = sc.nextLine();
if(userInput=="new"){
System.out.println("TRUE");
}else {
System.out.println("FALSE");
}
}
I have no idea why new not equals to new.
Please give me some hints :)
Use userInput.equals("new") instead.
This explains everything: How do I compare strings in Java?

'==' does not work while checking string values, but '.equals()'does. Can anyone tell me why? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
import java.util.Scanner;
public class RandomTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String[] alphabets={"a","b"};
String [] cipher={"b","c"};
System.out.println("Please enter a letter.");
String word=input.nextLine();
for (int i=0;i<2;i++){
if (word.equals(alphabets[i])){
System.out.println("Preparing a cipher");
System.out.println("Here is the cipher: "+cipher[i]);
}
}
}
}
This code above works perfectly fine, but instead of saying (word.equals(alphabets[i])), if i put word==alphabets[i] it wouldn't work at all. While using the later, the program does not check if the input is equal to a value in the array. Why does this happen?
== is an operator which compares the objects' locations in memory. equals() is a method defined in Object meant for comparing the actual content/values. By default, these two options behave similarly, but Java's String class overrides equals() when comparing strings.
In the future, please do research before asking questions. There is plenty of information to be found on the Internet.

IF condition on strings [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm writing a simple code to test the value that was inputted to my constant value.
I declared this code as my constant value.
String LetMeThrough = "drunk";
String GotAnID = "drunk";
This is the whole code.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner DrunkTest = new Scanner(System.in);
String InputDrunk;
String InputDrunkAgain;
String LetMeThrough = "drunk";
String GotAnID = "drunk";
System.out.print("Type drunk: ");
InputDrunk= DrunkTest.next();
System.out.print("Re Type drunk: ");
InputDrunkAgain = DrunkTest.next();
if(InputDrunk == LetMeThrough & InputDrunkAgain == GotAnID){
System.out.print("You're not DRUNK");
}
else
System.out.print("You're F***** DRUNK");
}}
The problem is that if I type "drunk" on both.
I will get "You're F****** DRUNK" instead of the "You're not DRUNK".
When the inputted values is the same as my constant values.
You must use String::equals method to compare.

Categories

Resources