Its supposed to notify the user if they have a palindrome, but keeps returning true even when I type, "hello".
import java.util.Scanner;
public class PalinDrome {
public static void main(String[] args) {
String line;
Scanner input = new Scanner(System.in);
System.out.print("Please enter a word ");
Is there a different logic in Java for if statements when it comes to conditions? I mean my attempt to compare a String variable and a String attribute of a class that is on an array of objects was frustrated someway. It will not enter the if block. The two strings are equal. I displayed the values of each strings before the if evaluation and they are equal. The simbol I used was the ==, and I also tried the string.equals(string variable) as well as the compareTo() == 0 option but none of those worked. I wish I knew what it is the way to compare two strings.
How would I create a equals method to compare strings in a class that I'm creating. I need to create the method in my class, and then call it in a driver. How would I do this?
I'm attempting a small program as I'm teaching myself the ropes. In it, I need to compare one string (the base) to another which is just the base string that's had it's characters shuffled.
Java Code:
String base = "ABCDEFG" String shuffled = "CDAFBEG" mh_sh_highlight_all('java');
What I need to do is run a loop that shuffles the base string each time, but compares and saves any characters that match in the correct location. For example, if shuffled = "CDAFBEG", then the G would be "locked" in place and the rest of the characters shuffled and looped back to the comparison.
I have all the code I need for shuffling the string, but I'm not sure how I would go about comparing each character and then also locking it in place. I get the basics, I think, of needed to use several variables.
I am having an array of strings and i want to find out whether these strings contained in the array contain a similar character or not.For example i am having following strings in the array of string:
aadafbd dsfgdfbvc sdfgyub fhjgbjhjd
my program should provide following result: 3 because i have 3 characters which are similar in all the strings of the array(f,b,d).
I can sort strings in a collection by uppercase and then lowercase though I was wondering if there is any way of doing it in reverse, sorting by lowercase then by uppercase.
I'm trying to get the input of two jcomboBoxes for example if jcombobox 1 option 1 and jcombox box 2 option 1 is selected result = x
Here is my code:
comboDest1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // // Get the source of the component, which is our combo box. // JComboBox comboDest1 = (JComboBox) event.getSource();
how can i compare date with following request the value date transction must be within the Active date(>= greater than or equal) and inactive date(<= less than) date range
public class Item{ private int myId; private int myInv; public Item(int id, int inv){ myId = id; myInv = inv;
[Code] .....
I do not understand how to complete the compareTo() method and the equals() method. I understand that the compareTo() to should return 1 if the argument is smaller -1 if its bigger and 0 if they are the same, but I do not understand how it's possible to compare the current object with the object passed in as the parameter.
This is what I tried for the compareTo():
public int compareTo(Item other){ return this.compareTo(other); }
But my compiler tells me its a recursive call so I am not sure if I am doing this correctly.
Basically I am supposed to compare an object to another but I am not sure how to access the object that is not passed in as a parameter and compare it. Same goes for the equals(). This is what I tried for that (compiler also says its a recursive call):
public boolean equals(Item other){ return this.equals(other); }
public class Circle extends GeometricObject implements Comparable<Circle> { private int radius; public Circle(int newRadius) { radius=newRadius;
[Code] ....
When I run the driver class it tells me that all tests have passed(as it should be) except for Test 0. I have tried to play around with the code, but I cant figure it out and it is driving me crazy because in my head my code makes sense.
I have a ArrayList of objects of class called HockeyPlayer (ArrayList<HockeyPlayer>). A HockeyPlayer has a String name and int number of goals.
This is my current work for comparing each object in the list to every other and printing them to screen:
Iterator<HockeyPlayer> it = hockeyPlayersList.iterator(); while (it.hasNext()) { HockeyPlayer singleHockeyPlayer = it.next(); //the first one encountered // HockeyPlayer nextHockeyPlayer = it.next(); //the next one encountered
[Code]...
This of course produces some duplication in the print-out: These are the equal hockey players: Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
These are the equal hockey players: Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
I could get my session values in my jsp, now I want to compare the session value whether it matches the textbox, if it matches, it will redirect the user to another page else it will remain the same page.
So I am trying to create a code that searches if a word is square free. The user inputs a word (into an array) and then the code is suppose to see if it is square free. A word being square free means that the word doesn't contain any consecutive sub words. For example, "abcabc" is not a square free word because abc is repeated, but "abcdabc" is a square free word because there is a "d" separating the "abc".
So far I have this :
import java.util.Scanner; public class A3Q2 { public static void main(String[] args) { // part (a) of the main Scanner keyboard = new Scanner(System.in);
[Code] ....
I've been trying to experiment with different ways such as checking to see if there any duplicate elements such as,
public static char isSquareFree(char[] word){ for(char i = 1; i < word.length; i++) { if(word[i] == word[i - 'a']) { System.out.println("Duplicate: " + word[i]); } } return word; } }
I want to make a program in which i write the Months Strings via while into the checkbox.
I already did that but i have also to add an day if February is a loop day.
So my question is how to say java that if Months is equal to February & year is a leap year, add 1. (i didn't wrote the year code because it's not relevant for my problem.)
Java Code:
public String[] Months ={"January","February","March","April","May", "June", "July", "August", "September", "Oktober", "November", "December"}; public Asg1KeapYear() { initComponents(); int MonthNo = 0;
[Code] ....
Netbeans shows me .equals() on incompatible types on Months.equals, do i have to declare it somehow?
>Suppose that aList and bList are instances of java.util.ArrayList. Use two iterators to find and display all the objects that are common to both lists. Do not alter the contents of either list. Write the segment of code assuming that the objects are of type String.
If my understanding is correct, I need to create two Iterators (one for aList and one for bList) to compare both ArrayLists and I should output any value that appears in both lists.
This is my attempt at it:
import java.util.ArrayList; public class Test { public static void main(String[] args) { ArrayList<String> aList = new ArrayList<String>(); ArrayList<String> bList = new ArrayList<String>();
aList.add("One"); aList.add("Two");
[Code] ....
The output:
`One`
`Two`
It stops there. It's clear that my loop is wrong, but I don't really know how to go about fixing it.