I'm trying to write a program that checks if a input string value contains any capital letters. Is there a way to do this in java?

make an array of chars, filled with all the possible capitals, iterate over this array and check if the string contains the char ...

the .contains method could do that for you

Using the char class would work, but for this class we're told we have to find a way to do it by creating a boolean method that uses 2 methods from the string class and a logical operator. Im pretty sure it employs the pattern method, but im still pretty stuck

If you just want a boolean to see if it had any capitals, you can use this:

String test1 = "Hello"// your string;
String test2 = test1.toLowerCase();
boolean hasCapital = !test1.equals(test2);

Edited 5 Years Ago by Aviras: n/a

i guess I should be a little more specific the boolean method needs to take a String argument and return true if it has at least one upper case letter and false otherwise

i guess I should be a little more specific the boolean method needs to take a String argument and return true if it has at least one upper case letter and false otherwise

heres my code thus far if this helps anyone understand my goal

public class Ex2 extends JFrame implements FocusListener {

	/**
	 * @param args

	 */	

	
	//clears text in userEntry when given focus 
	//and puts instructions back when not
	public void focusGained(FocusEvent e)
	{
		if(e.getSource() == userEntry)
		{userEntry.setText("");
		}}
	public void focusLost(FocusEvent e)
	{	
	if(e.getSource() == userEntry)
	{userEntry.setText("Enter your text here");
	}}
	
	private JButton checkCapButton;
	private JTextField userEntry;
	private JTextField Answer;
	public Ex2() {	
		super ("The Capital Check");
		setSize(400,400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		userEntry = new JTextField("Enter your text here");
		userEntry.addFocusListener(this);
		Answer = new JTextField(30);
		checkCapButton = new JButton("check it");
		checkCapButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Answer.setText("Has Capital: " + checkForCapitals(userEntry.getText()));		
			}
		});
		Container MyPane = getContentPane();
		MyPane.setLayout(new FlowLayout());
		MyPane.add(checkCapButton);
		MyPane.add(userEntry);
		MyPane.add(Answer);
		validate();

}
	
//		boolean checkForCapitals = Pattern.matches("ABCDEFGHIJKLMNOPQRSTUVWXYZ" userEntry.getText());
//		if (!checkForCapitals) Answer.setText("Your password does not contain Digits");
//		else Answer.setText("Your password does contain Digits");
//	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Ex2();
	}
}

i guess I should be a little more specific the boolean method needs to take a String argument and return true if it has at least one upper case letter and false otherwise

Which is exactly what I gave you..?

public static boolean checkForCapital(String s){
		return !s.equals(s.toLowerCase());
	}

It's the same, just put in a method.

For example "Hello" gets turned into "hello" by the method toLowerCase(), the equals function checks if they are...equal. Since "Hello" is not the same as "hello", it will return true, indicating that your string had a capital letter.

Edited 5 Years Ago by Aviras: n/a

Comments
this was the most helpful post, this user clearly knows what he is doing

jhamill here is how you can put Aviras' code into yours:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Ex2 extends JFrame {
	private JTextField data, output;
	private JButton go;

	public Ex2() {
		super("Looking for UPPER CASE");
		setSize(400, 400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		data = new JTextField("Enter your text here");
		output = new JTextField(30);
		go = new JButton("check it");
		go.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				output.setText("Has Capitals: " + 
						checkForCapitals(data.getText()));
			}
			protected boolean checkForCapitals(String text) {
				String test1 = data.getText();
				String test2 = test1.toLowerCase();
				if (test1.equals(test2))
					return false;
				else
					return true;
			}
		});
		Container myPane = getContentPane();
		myPane.setLayout(new FlowLayout());
		myPane.add(go);
		myPane.add(data);
		myPane.add(output);
		validate();
	}


	public static void main(String[] args) {
		new Ex2();
	}
}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

@CSCIHelper1111: You just gave him a function checkForCapitals which takes a String for an argument, but you don't even use it, and do what I did in 6 times as much code, while I even gave him the function which he just had to copy paste.

If you reply to a thread, first make sure what you want to say is correct, then check if you actually contribute something, which is why the poster is here.
</rant_over>

Edited 5 Years Ago by Aviras: n/a

haha you are so stupid
i gave him exactly what he asked for while you just posted 2 lines that he didn't know what to do with. you should be a little more comprehensive in your answers, but its okay I'm sure you're new at this. ill give you some slack

Comments
uncalled for, even besides the poster thinking otherwise
rude, uncalled for and proof that you don't respect the forum rules.
Personal insult.

haha you are so stupid
i gave him exactly what he asked for while you just posted 2 lines that he didn't know what to do with. you should be a little more comprehensive in your answers, but its okay I'm sure you're new at this. ill give you some slack

unlike you, he made an excellent point.
the OP was given several ways to solve his problem, without handing him over custom made code (which is what YOU did)

yes, your code will run (congratulations for that), but, it is exactly as you said:

i gave him exactly what he asked for while you just posted 2 lines that he didn't know what to do with.

so, you were "holding the baby by the hand" while Aviras was "guiding the child to walk on it's own". you don't help people learning, you just made his homework for him. great, he has working code now, he just doesn't know/understand how you got there, let alone how HE should have gotten there

package arrayexample1;
import java.util.Scanner;
public class StringCapitalExample {
    public static void main(String[]args)
    {
        char s2;
        Scanner sc=new Scanner(System.in);
        System.out.println("enter the string ....");
        String s1=sc.nextLine();
        int l=s1.length();
        for(int i=0;i<l;i++)
        {
            s2=s1.charAt(i);

                       if(s2=='A'|s2=='B'|s2=='C'|s2=='D'|s2=='E'|s2=='F'|s2=='G'|s2=='H'|s2=='I'|s2=='J'|s2=='K'|s2=='L'|s2=='M'|s2=='N'|s2=='O'|s2=='P'|s2=='Q'|s2=='R'|s2=='S'|s2=='T'|s2=='U'|s2=='V'|s2=='W'|s2=='X'|s2=='y'|s2=='Z')


                           System.out.println(s2+"is a capitalletter");

                       else
                       {
                           if(s2=='0'|s2=='1'|s2=='2'|s2=='3'|s2=='4'|s2=='5'|s2=='6'|s2=='7'|s2=='8'|s2=='9')
                           {
                               System.out.println(s2+"is a digit");
                           }

    else
    {
        System.out.println(s2+"it is not a  capital letter");
    }


                       }    


}
}
}

malli: please don't revive threads that were solved years ago.
I've seen two posts of you today, and my recommendation: be consistent in coding style:

either always put '{' and '}', or only put them when you have several lines in the block. you tend to mix them up, making the code even harder to read.
don't allow your main method to throw Exceptions (as you did in the last post of yours I've seen), there 'll be no way you'll be able to avoid a crash if something goes wrong. it's nice during dev, but not in a 'ready-made sollution', as you provided it there.

thirdly: it's nice you want to show off your knowledge of Java, but, besides the fact that there was no need to post code here: there are better ways to check whether or not a char is a digit or not, and that is not even part of what the OP was looking for, so why look for it?

are you truly going to check each char against 'A', 'B', ... ?
why not just compare two Strings, one, the original char (String value) and the second the same, but then using toUpperCase() ?
same result, much lesser code.

This question has already been answered. Start a new discussion instead.