Welcome to Dream.In.Code
Become a Java Expert!

Join 150,373 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,748 people online right now. Registration is fast and FREE... Join Now!




Help creating a isDigit and whitespace

2 Pages V  1 2 >  
Reply to this topicStart new topic

Help creating a isDigit and whitespace

kyleryansmith@hotmail.com
31 Jan, 2008 - 04:08 PM
Post #1

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 23


My Contributions
CODE
import javax.swing.*;
    
public class Question4
{
public static void main (String [] args) throws IOException
    {
    boolean test;
    System.out.println("Enter a character");
    test = Character.isLetter (letter);
    char letter = JOptionPane.showInputDialog.charAt(0);
    if ( Character.toUpperCase (letter) >= 'A' && Character.toUpperCase (letter) <= 'Z')
    {
    System.out.println( letter + " Is a letter");
    }
    else //[u] i don't know how to put the isDigit here?[/u]
    }
    System.out.println( letter + " Is a digit");
    }
       {
       //i have to create a white space here but don't know how to do that?

}
       //i could either put the isdigit is the else but i wanted to maybe put it in a separate method.
    public static boolean isDigit ( int Letter )
    
    {
    if ( letter = isDigit )
    {
    System.out.println ( letter + "Is a digit");
    }

// what i want this program to do is to test if letter is a letter, a digit or a white space, if its a digit i need the digit to be square rooted and then out putted.
User is offlineProfile CardPM
+Quote Post

alaub
RE: Help Creating A IsDigit And Whitespace
31 Jan, 2008 - 09:38 PM
Post #2

New D.I.C Head
*

Joined: 8 Jan, 2008
Posts: 23


My Contributions
QUOTE(kyleryansmith@hotmail.com @ 31 Jan, 2008 - 05:08 PM) *

CODE
import javax.swing.*;
    
public class Question4
{
public static void main (String [] args) throws IOException
    {
    boolean test;
    System.out.println("Enter a character");
    test = Character.isLetter (letter);
    char letter = JOptionPane.showInputDialog.charAt(0);
    if ( Character.toUpperCase (letter) >= 'A' && Character.toUpperCase (letter) <= 'Z')
    {
    System.out.println( letter + " Is a letter");
    }
    else //[u] i don't know how to put the isDigit here?[/u]
    }
    System.out.println( letter + " Is a digit");
    }
       {
       //i have to create a white space here but don't know how to do that?

}
       //i could either put the isdigit is the else but i wanted to maybe put it in a separate method.
    public static boolean isDigit ( int Letter )
    
    {
    if ( letter = isDigit )
    {
    System.out.println ( letter + "Is a digit");
    }

// what i want this program to do is to test if letter is a letter, a digit or a white space, if its a digit i need the digit to be square rooted and then out putted.


for a space try if (letter = " ")
for a digit try if(letter=>0 and letter=<9) then convert the string to an int and do your print line

don't just run the else as a digit becasue there are characters other than letters and numbers

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Help Creating A IsDigit And Whitespace
1 Feb, 2008 - 12:03 AM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
QUOTE
for a space try if (letter = " ")
for a digit try if(letter=>0 and letter=<9) then convert the string to an int and do your print line

I just want to warn that it is pseudo code, so simply typing it into Java won't yield the expected result.
User is offlineProfile CardPM
+Quote Post

kyleryansmith@hotmail.com
RE: Help Creating A IsDigit And Whitespace
1 Feb, 2008 - 12:57 AM
Post #4

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 23


My Contributions
QUOTE(1lacca @ 1 Feb, 2008 - 01:03 AM) *

QUOTE
for a space try if (letter = " ")
for a digit try if(letter=>0 and letter=<9) then convert the string to an int and do your print line

I just want to warn that it is pseudo code, so simply typing it into Java won't yield the expected result.


so what do you think i should do? as when i have been studying there is no line of code to show how a isDigit will look like so can you please supply me with a code for example.
Thanks guys for helping me out.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Help Creating A IsDigit And Whitespace
1 Feb, 2008 - 01:17 AM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I am sure you were shown how to write the aforementioned lines in Java.
Hints:
- a character literal for the letter a would be: 'a'
- to compare two characters, you can write 'a'=='b'
- to compare String you'd need: "a".compareTo(" ")
This should get you started.



User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Help Creating A IsDigit And Whitespace
1 Feb, 2008 - 01:24 AM
Post #6

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,167



Thanked: 45 times
Dream Kudos: 125
My Contributions
CODE


/* Please note that I haven't tried to remove all the problems. I just tried to add the logic for things you asked for.
    so it might be possible that I missed something which is wrong and you didn't mentioned here.  [maybe like taking inputs etc. ] */

import javax.swing.*;
import java.lang.Math;
    
public class Question4
{
public static void main (String [] args) throws IOException
    {
      int digit;
    System.out.println("Enter a character");
    char letter = JOptionPane.showInputDialog.charAt(0);
    if ( Character.toUpperCase (letter) >= 'A' && Character.toUpperCase (letter) <= 'Z')
    {
    System.out.println( letter + " Is a letter");
    }
    else if(letter == ' ')  // You can compare it directly as it's a character datatype
    {
      System.out.println( letter + " Is a Whitespace");
    }
    else if(Character.isdigit(letter))   // I don't think you need to create any new function for this.
    {
      System.out.println( letter + " Is a digit");
      digit = Integer.parseInt(Character.toString(letter));
      System.out.println( "Square Root of " + letter + " is " + Math.sqrt(digit));
     }
   }
}



I have tried to correct things for you. please ignore the syntax errors if any. I hope this will help you.
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Help Creating A IsDigit And Whitespace
1 Feb, 2008 - 01:36 AM
Post #7

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
-- deleted -- people always reply while I'm typing mine. grrr

NB:
= is an assignment operator. You don't want to use it inside an if statement.
To compare, always use ==

e.g.
if(number = 5) { ... } WRONG
if(number == 5) { ... } Correct

This post has been edited by Nayana: 1 Feb, 2008 - 01:41 AM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Help Creating A IsDigit And Whitespace
1 Feb, 2008 - 01:41 AM
Post #8

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,167



Thanked: 45 times
Dream Kudos: 125
My Contributions
QUOTE(Nayana @ 1 Feb, 2008 - 03:06 PM) *

-- deleted -- people always reply while I'm typing mine. grrr



biggrin.gif This happened second time between us. Next time check if I am there on the same question before answering it. tongue.gif biggrin.gif
User is offlineProfile CardPM
+Quote Post

kyleryansmith@hotmail.com
RE: Help Creating A IsDigit And Whitespace
1 Feb, 2008 - 04:11 AM
Post #9

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 23


My Contributions

Thanks alot and Nayana.
As concerning to the other guy, i was not shown as i do not understand to how to look it up on the net, and when i do its a meaning not a example.
But thanks guys i will see if it will work and then i will add some and so on, but thanks its a real help, as i have to study alone so its nice to see that smarted guys are willing to help.
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Help Creating A IsDigit And Whitespace
2 Feb, 2008 - 11:46 PM
Post #10

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
AmitTheInfinity: I will do so next time aye ;-)

It really p*sses me off when I've gone through the effort to do something, and then you've done it first :-) Haha.

Mr. Smith: You may want to change your nick name so that it is not your email :-) Good luck with your studies.
User is offlineProfile CardPM
+Quote Post

kyleryansmith@hotmail.com
RE: Help Creating A IsDigit And Whitespace
4 Feb, 2008 - 07:39 AM
Post #11

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 23


My Contributions
[/quote]
How can i change my nick name?

[/quote]

User is offlineProfile CardPM
+Quote Post

kyleryansmith@hotmail.com
RE: Help Creating A IsDigit And Whitespace
4 Feb, 2008 - 08:07 AM
Post #12

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 23


My Contributions
[code]
import javax.swing.*;
import java.lang.Math;

public class Question4
{
public static void main (String [] args)
{
boolean test;
int digit;
char letter = JOptionPane.showInputDialog("Enter a character").charAt(0);
if (test= Character.isLetter(letter) )
{
System.out.println( letter + " Is a letter");
}
else if (test= Character.isWhitespace(letter)) // thanks guys for all the help, but i just can't get this white space to work.
{
System.out.println( letter + " Is a Whitespace");
}
else if (Character.isDigit(letter))
{
System.out.println( letter + " Is a digit");
digit = Integer.parseInt(Character.toString(letter));
System.out.println( "Square Root of " + letter + " is " + Math.sqrt(digit));
}

}

}
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:41PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month