This question already has answers here:
How does compareTo work?
(3 answers)
How to correctly compute the length of a String in Java?
(5 answers)
Closed 3 months ago.
This code is to compare 2 strings entered by the user and it's supposed to output whether the first string is > < or = the second string.
It's working for the most part except when I enter two phrases like gh and hi, it thinks gh is greater than hi. Maybe it's looking at the size of the actual letter.
package com.mycompany._3;
import java.util.Scanner;
public class App {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first phrase(s): ");
String one = scanner.next();
System.out.print("Enter Second phrase(s): ");
String two = scanner.next();
int length = one.compareTo(two);
if(length > 0){
System.out.print("String one is less than string two.");
}else if (length < 0)
System.out.print("String one is greater than string two.");
else
System.out.print("Both phrases are equal length");
}
}
The compareTo() method compares two strings lexicographically.
The comparison is based on the Unicode value of each character in the strings.
You need to call length() method for each string to compare lengths
if(one.length() > two.length()) ...
Related
This question already has answers here:
why cant we use <, <=, >,>= relational operators on String?
(4 answers)
Closed 1 year ago.
I have no idea why this doesn't work. Please help, I am new to java
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("what is your first number ");
String firstNum = scanner.nextLine();
System.out.println("what is your second number ");
String secondNum = scanner.nextLine();
System.out.println("what is your Third number ");
String thirdNum = scanner.nextLine();
if (firstNum > secondNum) {
System.out.println("the firs number is bigger than the firs number");
Scanner.nextLine() is used to input string . Instead use scanner.nextInt() to input number and store in an integer variable.
This is an illegal comparison. You cannot compare a String to a String. You should use Scanner.nextInt(). If you want to use a string to store a numerical value than you can use Integer.parseInt() to convert string into int.
This question already has answers here:
How to format a Java string with leading zero? [duplicate]
(24 answers)
Closed 5 years ago.
So my program is meant to take user input which is supposed to be a number from 1-511. Once the number is entered my program uses Integer.toBinaryString(); to turn their number into the binary form of that number. Then, if the binary output is not 9 digits, I want to fill the rest of that binary number with 0's until its 9 digits long. So I use an if statement to check if the length of the binary is less than 9, if it is I have it set to subtract 9 from binary.length so we can know how many zeros we need to add. Im currently stuck so I just made a print statement to see if its working so far and it does. But im not sure what to do as far as finishing it.
import java.util.Scanner;
public class Problem8_11 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter one number between 0 and 511: ");
int number = input.nextInt();
int binaryZeros = 9;
String binary = Integer.toBinaryString(number);
if(binary.length() < 9) {
int amountOfZeros = 9 - binary.length();
System.out.print(amountOfZeros);
}
}
My question: How do I add the zeros to binary?
Presumably you want to prepend the zeros. There are many ways to do this.
One way:
StringBuffer sb = new StringBuffer( Integer.toBinaryString(number) );
for ( int i=sb.length(); i < 9; i++ ) {
sb.insert( 0, '0' );
}
String answer = sb.toString();
Be sure to handle the case where the number cannot be parsed into an integer.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
public class ex1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Please enter a series of strings each followed by the enter key. When you'd like to end thr program simply type 'quit': \n");
Scanner scan = new Scanner(System.in);
ArrayList<String> inputList = new ArrayList<String>(); // creates a list to store user input
String input = scan.nextLine(); //takes the scanner input
while(input != "quit") { //makes sure its not equal to quit
//System.out.println(input);
inputList.add(input);
input = scan.nextLine();
}
scan.close();
System.out.println("The number of strings enetered was: " + inputList.size());
System.out.println("The strings you entered were as follows");
for (String i: inputList) {
System.out.println(i);
}
}
}
I'm trying to use the preceding code to take a series of inputs from a user using the enter key and if they enter quit I end the program. However the condition is never satisfied and the while loop never ends and I can't understand why
while(!input.equals("quit")) { //makes sure its not equal to quit
//System.out.println(input);
inputList.add(input);
input = scan.nextLine();
}
You should use equals method as shown above to compare strings. Java provides equals method to compare the contents of two strings. == and != operators are used in comparing object equalities.
a == b returns true if, and only if, a points to the same object as b
The equals method should be used, as the String class implements it so that, if a contains the same characters as b, it would returns true.
while (!input.equals("quit")) { ... }
I need to create two different programs. One that does the following:
Enter your first number: 15
Enter your second number: 25
25 is larger than 15
and a second separate one that does the following:
Enter the first string: apple
Enter the second string: bananas
apple comes before bananas lexiographically
This is what I tried for the first one:
import java.util.Scanner;
public class ClosedLab03 {
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter your first number: ");
int firstNumber = keyboard.nextInt();
System.out.println("Enter your second number: ");
int secondNumber = keyboard.nextInt();
int result;
if (firstNumber > secondNumber)
{
result = System.out.println(firstNumber +" is larger than " + secondNumber);
}
else
{
result = System.out.println(secondNumber + " is larger than " firstNumber);
}
Obviously I'm doing something wrong, but I don't really know what. In terms of comparing the strings, I really don't know how to compare them lexicographically. Our textbook shows us how to compare two strings and say whether or not they are the same, but not how to compare them and display which one is lexicographically first.
String.compareTo(String) does this for you. It implements the java.lang.Comparable interface.
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
Example
System.out.println("apples".compareTo("bananas")); // returns -1
System.out.println("bananas".compareTo("apples")); // returns 1
System.out.println("apples".compareTo("apples")); // return 0
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
When I create a variable and wrap the code in a while loop it never repeats. Here's a sample of code I tried it on.
String repeat = "y";
Scanner keyboard = new Scanner(System.in);
while (repeat == "y"){
String word1 = "this";
String word2 = "that";
String word3 = word1 + word2;
System.out.println(word3);
for(int x = 10; x<20; x = x+1){
word3 = word1 + word3;
System.out.println(word3);
}
repeat = keyboard.nextLine();
}
No matter what the input is in the end of the script, it just ends. Any help?
Change the line
while (repeat == "y")
to
while("y".equalsIngnoreCase(repeat))
and
keyboard.nextLine() ;
to
keyboard.next();
Reading How do I compare strings in Java? will be helpful.
When you compare a string using '=='. you are comparing the object reference. You are essentially asking if the two objects are the same, rather than comparing the string contents. Try using the String.compareTo(..) method.
Example:
while (repeat.compareTo("y") == 0) {
The problem probably comes from the facts you compare two objects with = but I guess what you want to do is compare Strings with the String method equals, so it would look like that:
while(repeat.equals("y")){
...
}
Never use == to compare strings.
Try this:-
while ("y".equalsIngnoreCase(repeat) )