Boolean, comparing user string & user char

I'm missing something. i'm trying to compare a string, from user input, to a char, from user input. Ex. string, char: "sssss" "s". Solving for does string "sssss" contain only char "s" ? In this case, should return true. In "ssss sd" "s" SOP should retun false false. I'm having trouble writing code so that string "s", however many, = char "s". Probably confusing, I'm sorry. Here's what I have:
//     StringInput.java          
public class StringInput
     public static void main(String[] args)
     String s = args[0];          //get the String from the user
     char c = args[1].charAt(0);     //get the char from the user
     int i = 0;
     boolean answer = true;{
     while (answer && (i < s.length () - 1)) {
//char c = s.charAt (i);
     //if (! (((c >= '0') && (c <= s.length)) || (c == ' ')))
     answer = false; }}
     String trim = s.trim();
     s = trim;
     String replace = s.replace('a', 's');
     int length = s.length();
     System.out.println (trim);
     System.out.println (replace);
     System.out.println (length);
System.out.println("The String " + s + " is composed " + " entirely of the char " + c + ": " + answer);                          

This might work
class ScanInput
     public static void main(String[] args)
          String s = args[0]; //get the String from the user
          char c = args[1].charAt(0); //get the char from the user
          boolean answer = false;
          int i =0;
          while ( (! answer) && ( i < s.length( ) -  1 )  )
                    if ( c != s.charAt( i ) )
                              answer = true;
                    i++;
          if ( answer)
                    System.out.println(" The input string " + s + " contains other characters besides " + c );
          else
                    System.out.println(" The input string " + s + " contains only character " + c );
}

Similar Messages

  • Compare two strings in an array

    Hello!
    Please feel free to give me hints, but do not give me any code.
    Ok, here is what I am trying to do, I whant to compare my input string whith strings allready stored in the array. If my input string equals any of the allready excisting strings, a error wil show upp, otherwise store the new string in next avalibal position. I do not seem to get the method right for comparing the strings. here is the method I have written so far, please take a look and give me a hint, but no code. :)
    //Check if a user already excists     
    public void compareNames (People in_array [], String in_name) {
              for (int i=0; i<value.peopleCount; i++) {
                   if (in_name.equals(in_array.getPeople())) {
                   System.out.print("The user already excist, please chose another name");
    }value.peopleCount is an global count value for people arays.
    getPeople get the name from the people class.
    Martin                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    A couple notes here.
    The name compareNames() is misleading if it is going to do what you described. A comparison will generally not have side-effects like outputting error messages to the console or adding new items to an array. It would be better if you called the method addIfNew(), and returned a boolean indicating whether the name was new or not. The caller would then be responsible for displaying an error message if the method returned false.
    I also suggest you use a List such as ArrayList instead of an array, otherwise you will have to resize and copy your array every time you add something to it that exceeds the array size, and will allow you to do away with the global peopleCount.

  • Compare two string in different line in textarea

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TextCounterPanel extends JPanel {
    private JTextArea textInput;
    private JLabel lineCountLabel;
                        public TextCounterPanel() {
                        setBackground(Color.DARK_GRAY);
                        textInput = new JTextArea();
                        textInput.setBackground(Color.WHITE);
                        JPanel south = new JPanel();
                        south.setBackground(Color.DARK_GRAY);
                        south.setLayout( new GridLayout(4,1,2,2) );
                             JButton countButton = new JButton("Process the Text");
                             countButton.addActionListener( new ActionListener() {
                                  public void actionPerformed(ActionEvent evt) {
                                       processInput();
                             south.add(countButton);
                        lineCountLabel = new JLabel(" Number of lines:");
                        lineCountLabel.setBackground(Color.WHITE);
                        lineCountLabel.setForeground(Color.BLUE);
                        lineCountLabel.setOpaque(true);
                        south.add(lineCountLabel);
                        setLayout( new BorderLayout(2,2) );
                        setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
                        JScrollPane scroller = new JScrollPane( textInput );
                        add(scroller, BorderLayout.CENTER);
                        add(south, BorderLayout.SOUTH);
    public void processInput() {
    String text,vin; // The user's input from the text area.
    text = textInput.getText();
    vin =text.substring(25,42);
    lineCountLabel.setText(" vin: " + vin);
    } // end class TextCounterPanel
    How can I compare two string in different line in text area

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TextCounterPanel extends JPanel {
    private JTextArea textInput;
    private JLabel lineCountLabel;
    public TextCounterPanel() {
    setBackground(Color.DARK_GRAY);
    textInput = new JTextArea();
    textInput.setBackground(Color.WHITE);
    JPanel south = new JPanel();
    south.setBackground(Color.DARK_GRAY);
    south.setLayout( new GridLayout(4,1,2,2) );
    JButton countButton = new JButton("Process the Text");
    countButton.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    processInput();
    south.add(countButton);
    lineCountLabel = new JLabel(" Number of lines:");
    lineCountLabel.setBackground(Color.WHITE);
    lineCountLabel.setForeground(Color.BLUE);
    lineCountLabel.setOpaque(true);
    south.add(lineCountLabel);
    setLayout( new BorderLayout(2,2) );
    setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
    JScrollPane scroller = new JScrollPane( textInput );
    add(scroller, BorderLayout.CENTER);
    add(south, BorderLayout.SOUTH);
    public void processInput() {
    String text,vin; // The user's input from the text area.
    text = textInput.getText();
    vin =text.substring(25,42);
    lineCountLabel.setText(" vin: " + vin);
    } // end class TextCounterPanel

  • Compare two strings in a formula

    Hi,
    I'd like to know if there's a way to compare two strings in a formula ?
    I have this consition:
    IF(@SUBSTRING(@NAME(@CURRMBR(TIME_FROM)),5)==@NAME(@CURRMBR(TIME)))
    But the == operator can only compare two data.
    Thanks,
    Cyril

    Hi,
    thank you both for your answers.
    When I figured that the == operator was for data only, I didn't even try to compare member names.
    I finally used the solution that Cameron suggested in another conversation. I created a date measure, and loaded some data since the beginning of my time to the end.
    It starts in 1974 (annually), then becomes monthly data, and then daily data from 2009, so it's not so many blocks to br created. In the end I have the same value for "from 2008"/"from Apr 30", and I can easily compare my dates.
    Note that I could have also used the @TODATE function, which turns a date into a number, but the format of date is limited and doesn't fit the format of my member names, so I got lasy... and I'm not sure my users would have apreciated that ;)
    Thanks, this post is solved.
    Cyril

  • Compare two strings for partial or full match

    I have been trying to figure out away of comparing two strings.
    I can get the full string matching to successfully work but am struggling on how to get partial eg wildcard search working.
    say I had names like paul Duncan, George Morrison.
    I have split them up so its now paul , duncan i then use the users entered string to compare it against the name name to see if it matches anything in the first name or the last name if it does i added it to an arraylist.
    however i have tried t use a indexof as i believe this looks for similarity (Substring matches?) but it always returns -1.
    Could anyone possibly direct me to a method that will allow me to use partial text string to match it against stored user names.
    eg user enters pa
    I would return anything that would contain pa
    thanks for any help

    thank you for the reply
    Sorry I forgot to put in the code.
    This is a reduced down version including the read in from a text file as well as the index of if statement and the adding of it to an array list.
    public class FindContact {
    String res;
    /** Creates a new instance of FindContact */
    public FindContact() {
    public Object[] FindAUser(String passSearch) {
    System.out.println("getting here");
    ArrayList found = new ArrayList();
    String patternStr = ",";
    int j, i = 0;
    try {
    BufferedReader in = new BufferedReader(new FileReader("H:\\out.txt"));
    String str;
    while ((str = in.readLine()) != null) {
    String[] fields = str.split(patternStr);
    //for(j=0;j < fields.length;j++) {
    if (fields.length != 0) {
    if (passSearch.indexOf(fields[0])!=-1 || passSearch.indexOf(fields[1])!=-1) {
    found.add(str);
    System.out.println("ENDDDDDDDD value ");
    in.close();
    } catch (IOException e) {
    System.out.println(e.toString());
    Object[] foundResult = new Object[found.size()];
    foundResult = found.toArray();
    if (foundResult.length >= 0) {
    System.out.println(foundResult.length);
    return foundResult;
    Hope that helps as it has confused me and am not sure why it is not returning a value.
    Should I maybe use the contain method instead to search for the user inputted string?

  • Comparing 2 strings

    hi
    i have an application, in which, i am taking a string as input from th user(thru a JOptionpane.inputDialog)
    now, i have to compare this string with various other values present in an already stored file.
    please sugest a method to do so.
    the file has different values appended one after another , separated by a space.
    the problem is, i want to parse the entire file, to look for the required value.
    please help.
    thank u.

    Parse the file once using a thread as soon as the program has started running.
    If you just want to know whether or not a value is in the file then just store the values from the file in a Set (java.util.HashSet).
    If you want to associate a value with some other information then store the (key,value) pairs read from the file in a Map (java.util.HashMap).
    If you want to record information about what the user has entered then again store the file informaion in a Map but make the value an object of your own making.

  • How to compare two strings whether both are equal while ignoring the difference in special characters (example: & vs & and many others)?

    I attempted to compare two strings whether they are equal or not. They should return true if both are equal.
    One string is based on Taxonomy's Term (i.e. Term.Name) whereas other string is based on String object.
    The problem is that both strings which seem equal return false instead of true. Both string values have different special characters though their special characters are & and &
    Snapshot of different design & same symbols:
    Is it due to different culture or language?
    How to compare two strings whether both are equal while ignoring the difference in special characters (& vs &)?

    Hi Jerioon,
    If you have a list of possible ambiguous characters the job is going to be easy and if (& vs &) are the only charracters in concern awesome.
    You can use the below solution.
    Before comparing pass the variables through a replace function to standarize the char set.
    $Var = Replace($Var,"&","&")
    This is going to make sure you don't end up with ambiguous characters failing the comparison and all the char are "&" in this case.
    Similar technique is used to ignore Character Cases 'a' vs. 'A'
    Regards,
    Satyajit
    Please “Vote As Helpful”
    if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

  • String to Char?

    Hi, sorry, I'm new to Java so this may sound like a silly question. I have found how to convert from a String to an Int (with Integer.parseInt() ) - can anyone tell me what the easy equivalent is for String to Char (I want to assign the value of a String to a Char variable).
    Many thanks.
    Traci

    Try string.charAt(0). Don't forget to check that the string should contain at least one char for that to work.

  • How to Compare two strings in PL/SQL

    Hi All,
    I need to compare two strings whether they are equal or not in PL/SQL.Is there any function to comparing the strings.

    Yes, the = sign.
    Are you after something like:
    IF v_string1 = v_string2 THEN
    ELSE
    END IF;?
    Edited by: Boneist on 27-Aug-2009 11:41

  • Comparing  NVL strings in IF statement

    ORA-06550: line 22, column 12: PLS-00412: list of values not allowed as argument to this function or procedure ORA-06550: line 22, column 1: PL/SQL: Statement ignored
    if     nvl((lower(v_address),'null') not like (lower(:ADDRESS)))

    Hi,
    Lucy Discover wrote:
    Whats the valid syntax to compare 2 strings . I want to compare it even if the first string is null.This is the correct syntax
    if     nvl (lower (v_address), 'null') not like lower (:ADDRESS)assuming V_adress and and :address are strings (and not some kind of collection, even if made up of strings).
    I hope this answers your question.
    If not, post a complete procedure or block, a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, so that people can re-create the probnlem and test their ideas. Also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.

  • Comparing 2 strings in if statement

    Hi All,
    I want to compare two strings in if statement. When I use "==" then its throwing error that expected number and got string. Can anyone please let me know how can we compare two strings in an IF statement?
    Thanks in advance

    The OP doesn't state what release he's on.
    The functions @LIKE and @MBRCOMPARE are 11.1.2 and up only.
    OP, if you're at a lower release, I've faced this problem as well in 11.1.1.3. Here's an older thread that detailed how I got around it for a client:
    Re: How to compare two strings in calc script?
    It really wasn't hard to do and the performance, in the limited context I was using it in, was fine.
    Regards,
    Cameron Lackpour

  • How to compare German strings with umlaut

    hi
    here is a solution for the problem to compare German strings containing umlaut, which I faced and where I did not find any solution yet!
    The right thing is to use the RuleBasedCollator class and define the additional rules (concerning the umlaut) by yourself and setting the strength of the rule interpretation to PRIMARY!
    Here is the code that works:
    public class UmlautTest { 
    public static void main (String args[]) {    
    String st1 = "Daettenb�ck";
    String st2 = "D�ttenbueck";
    String myRule = "< ae,�,AE,� < oe,�,OE,� < ss,�,SS < ue,�,UE,�";
    RuleBasedCollator myrbCol = null;
    try {
    myrbCol = new RuleBasedCollator(myRule);
    } catch (ParseException e) {
    e.printStackTrace();
    myrbCol.setStrength(Collator.PRIMARY);
    if (myrbCol.compare(st1, st2) != 0) {
    System.out.println("myrbCol: NICHT gleich!");
    } else {
    System.out.println("myrbCol: Gleich!");
    I hope that I could help anyone with that
    nocomm

    The � should be of course replaced by the missing umlaut!
    Sorry, but I didn't preview the topic!
    nocomm

  • String to char conversion

    How do I convert a string I have (I am sure its one character) in to char?

    you must mean char foo, instaead of String!
    char foo = "bar".toCharArray()[0];

  • Write string (7 chars), int16, sgl into binary file and read it in C

    How to write write string (7 chars), int16, sgl, string (5 chars) into binary file and then read it in C ?
    Total 18 Bytes file (binary) should be created.

    Hi,
    this could be done that way:
    You have to make sure the strings have their correct length!
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

  • Compare two strings and the save the greater

    To devolop a vi,it can compare two strings.A string is gived,the other is saved previously.If greater,then save,or do nothing.

    Hi,
    I am assuming by "greater" you mean the string length.
    Use the string length function from the strings palette, with shift-registers on a while loop. Write to the shift-register the "greater" string.
    I am attaching a diagram screenshot for this. NOTE: depending on how you want this VI to run, you can move the input and output string control and indicator inside our outside the while loop.
    Hope this is what you were looking for.
    Khalid
    Attachments:
    strlen.gif ‏17 KB

Maybe you are looking for

  • JDBC with SQL Server

    Hi, I am trying to connect to a SQL server database by giving table name in (SPVC) database as spvc.dbo.tablename.. If i want to access another database (SPVC_D), how can i do it without changing my java code?? Can i be able to add the database name

  • Error while executing action SMCO in VMS Build-to-Order process.

    Hello All, When I execute the Action 'SMCO' for service order confirmation, the system says that the action cannot be executed due to the error: "Actual date(s) is in the future (check parameters)". Also the Actual start & End Dates are blank in the

  • Can a document being protected from editing in a Sharepoint document library?

    Hi everyone, I have a document library in Sharepoint 2010 where users may upload documents. The goal is that users may see the documents but not edit them. How can i get the doc library so that users may upload documents, but can't edit any of the do

  • ORACLE 9iR2 OMS On SuSE 8

    I have installed ORACLE 9i R2 on SuSE with no errors.And I have configured my OMS with no errors.But when I use "oemctl start oms" to start OMS,it said "Time out to start oms,......checking oem.nohup".I opened the oem.nohup,it said /usr/bin/nice /opt

  • SG300-28 SSH incoming packet was garbled on decryption

    I use PuTTY to SSH into Cisco switches regularly. Today I ran into the message 'incoming packet was garbled on decryption'. I logged into the switch via HTTP and saw the uptime was 115 days. After seeing I could PuTTY into the SG-300-28P right above