how do I print ONLY upper case letters

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 8162 Views - Last Post: 04 October 2010 - 06:50 PM Rate Topic: -----

#1 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

how do I print ONLY upper case letters

Posted 03 October 2010 - 03:04 PM

Hi all,
I am stumped on one of my programs that has a lot of methods on it and the last one is one to print out only the upper case letters in a given input. Now I know converting to upper case is .touppercase or w/e BUT how do I print out from a given input?
As you can see I just have the method as a comment.
import java.util.*;
public class loopLab3
{
  public static void main(String[] args)
  {
    //all of the methods that will be used, divided up so it is easier for me to read this way
    incCount();
    decCount();
    evensCount();
    loveJava();
    promptJava();
    //upperCase();
  }
  //counting and printing 1 - 10
  public static void incCount()
  {
    //declaring the counter and the initial value
    int count;
    count = 1;
    //while statement that keeps printing numbers until 10 is the last number printed
    while (count != 11)
    {
      System.out.println(count);
      count++;
    }
  }
  // count and printing 10 - 1
  public static void decCount()
  {
    //declaring counter and initial value
   int deCount;
   deCount = 10;
   //while loop that gives a decremental value that reduces the number from 10 to 1 and doesnt go further
   while (deCount != 0)
   {
     System.out.println(deCount);
     deCount--;
   }
  }
  //counts even numbers up to the number 20
  public static void evensCount()
  {
    //declaring counter and initial value
    int evenCount;
    evenCount = 2;
    //while loop that continues to count until it hits 20
    while (evenCount != 21)
    {
      //if statement within while that prints even numbers only
      if (evenCount % 2 == 0)
      {
      System.out.println(evenCount);
      }
      evenCount++;
    }
}
  //prints the statement 50 times
  public static void loveJava()
  {
    //declares counter and initial value
    int javaCount;
    javaCount = 0;
    //while loop that only prints 50 times
    while (javaCount != 51)
    {
      System.out.println("I love java!");
      javaCount++;
    }
  }
  //prints statement user defined number of times
  public static void promptJava()
  {
    //declares variables and input
    int promptNum;
    Scanner console = new Scanner(System.in);
    System.out.println("Enter number of times you want to print");
    promptNum = console.nextInt();
    while (promptNum > 0)
    {
      System.out.println("I love java");
     promptNum--;
    }
  }
 
}



Is This A Good Question/Topic? 0
  • +

Replies To: how do I print ONLY upper case letters

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10437
  • View blog
  • Posts: 38,651
  • Joined: 27-December 08

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 03:16 PM

It sounds like you already know the answer:
-get input as a String
-use the toUpperCase() method
-output the new String

Also, not good practice to declare a Scanner in each method. If you need to get input for the printUpperCase() method, declare one Scanner and use that in your program.
Was This Post Helpful? 1
  • +
  • -

#3 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 03:58 PM

Oh wow so all this looking around and the toUpperCase can pick out all of the uppercase letters in an input and then be able to output just those letters?
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 04:35 PM

Yup It's as simple as this:
System.out.println(stringObj.toUpperCase());


Was This Post Helpful? 1
  • +
  • -

#5 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 04:55 PM

Well that didn't work as planned.
It just prints everything into upper case.
Example: I enter HeLLo ThERe
Program while loop should then print out LLTER since those are the only upper case letters.
This is why I am having such a hard time with this While Loop.
There are a dozen things on making things upper case but nothing on printing ONLY the upper case letters out of an input.
Not sure at this point if its even possible.
Was This Post Helpful? 0
  • +
  • -

#6 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 05:01 PM

I would imagine, then, that you would want to do something like this:

Get input
Decide which input letters are capitalized
Return only those letters

I think this is a better approach that try to remove lower case characters from a string and then returning the string.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 05:04 PM

OH! I thought you wanted to output in all uppercase. My bad...

A good way to approach this is to cycle through the String letter by letter and then use the Character.isUpperCase(char) method to check. If it is, print it.

for (int i = 0; i < str.length; i++) {
    char currentLetter = str.charAt(i);
    if (Character.isUpperCase(currentChar)
        System.out.print(currentLetter);
}
System.out.println();



you use the String.charAt() method to grab the current character as you cycle through, check it to see if it's uppercase, and if so, print it.
Was This Post Helpful? 1
  • +
  • -

#8 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 05:40 PM

isUpperCase! just what i was looking for but oddly found nowhere through google searching. TYVM!!!!
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 05:49 PM

You're very welcome. Glad I could help. :)
Was This Post Helpful? 0
  • +
  • -

#10 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 06:29 PM

Ok a lot of the examples I am finding on how to fix this problem all involve boolean. I don't want boolean.
Not entirely sure how to make what I want into a while loop either but I am certain it can be done.
public static void upperCase()
  {
    String input;
    System.out.println("Enter the text");
    Scanner console = new Scanner(System.in);
    input = console.next();
    while(input.isUpperCase > 0)
         System.out.print(input.isUpperCase);
 }


Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 06:33 PM

What is that? isUpperCase() is a method of the Character class, not the String class. You are also forgetting the parentheses. It returns a boolean, whether or not you want it to and honestly I don't know why you'd want it to return anything but. Yes, it can be done with a while loop, but again, you need the charAt() method or use the toCharArray() method.
Was This Post Helpful? 0
  • +
  • -

#12 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 06:44 PM

Well ....Building Java Programs: A Back to Basics Approach was obviously not worth the money.....I shall continue tinkering with my program..... I guess at least I have all the other while loops correct besides that upperCase on.....good to take solace in the small things.
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: how do I print ONLY upper case letters

Posted 03 October 2010 - 06:45 PM

Good luck! If you need help, feel free to post! We're all happy to help. :)
Was This Post Helpful? 0
  • +
  • -

#14 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: how do I print ONLY upper case letters

Posted 04 October 2010 - 06:11 PM

Ok here is what I got so far since I was able to get some sleep and such. It still won't work properly but I am sure that I am getting close to the answer.
Any help would be welcome plz
public static void upperCase() {
    Scanner console = new Scanner(System.in);
    System.out.println("Enter text:");
    String userInput = console.next();
    int letterCount = 0;
    while(letterCount < userInput.length()) {
      char currentLoc = userInput.charAt(letterCount);
      if (Character.isUpperCase(currentLoc)) {
        System.out.print(currentLoc + " ");
        letterCount++;}
      else {
        letterCount++;
      }
    }
   
      System.out.println("Enter text:");
      userInput = console.next();
    }


Was This Post Helpful? 0
  • +
  • -

#15 Grim-XTO  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: how do I print ONLY upper case letters

Posted 04 October 2010 - 06:25 PM

Ok what is wrong with this is that if I include a space it stops at the space and also I cant figure out how to get this so that it ends the loop.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2