CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2009
    Posts
    52

    Basic java problem

    Hi i need some help to implement the limit of characters one can type. It should show an error msg that the user cannot input more than 2 characters in x coordinate and y coordinate. How should i do that?

    import java.util.Scanner;

    public class P1
    {
    public static void main(String[]args)
    {
    int xaxis,yaxis;
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter the X Coordinates:");
    xaxis = sc.nextInt();
    System.out.println("Enter the Y Coordinates:");
    yaxis = sc.nextInt();
    if(xaxis >=0 && yaxis >=0)
    System.out.println("Q1");
    else if(xaxis < 0 && yaxis >=0)
    System.out.println("Q2");

    }
    }

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: Basic java problem

    First you should consider what happens if the user enters a wrong input? How do you get to ask for a new one? As to limiting things to simply 2 characters... how is that difficult? Digits smaller than -99 and greater than 99 have more than "2 characters".. why not check that?

  3. #3
    Join Date
    Aug 2009
    Posts
    52

    Re: Basic java problem

    Quote Originally Posted by Londbrok View Post
    First you should consider what happens if the user enters a wrong input? How do you get to ask for a new one? As to limiting things to simply 2 characters... how is that difficult? Digits smaller than -99 and greater than 99 have more than "2 characters".. why not check that?
    Hi, Thanks for the advice thats a good point, however could you give me more specific instructions as i am quite a newbie in java. Thanks

  4. #4
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Basic java problem

    Code:
    int strlength = str.length();
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  5. #5
    Join Date
    Aug 2009
    Posts
    52

    Re: Basic java problem

    Quote Originally Posted by Xeel View Post
    Code:
    int strlength = str.length();
    It gives me this error

    P1.java:21: int cannot be dereferenced
    int strlength = xaxis.length();
    ^
    1 error

  6. #6
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: Basic java problem

    u meaned that??


    if(xaxis >=0 && xaxis<=99 && yaxis >=0 && yaxis<=99)
    System.out.println("Q1");
    else if(xaxis < 0 && xaxis>=-99 && yaxis >0 && yaxis<=99)
    System.out.println("Q2");

  7. #7
    Join Date
    Aug 2009
    Posts
    52

    Re: Basic java problem

    Im actually planning to do something like that. however i had error reading the length

    int strlength = xaxis.length();
    if ( strlength > 2) {
    System.out.println("Pls input again");
    System.exit(0);
    }
    else
    ....

  8. #8
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: Basic java problem

    Code:
    import java.util.Scanner;
    
    public class P1
    {
    public static void main(String[]args)
    {
    String xaxis,yaxis;
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter the X Coordinates:");
    xaxis = sc.next();
    System.out.println("Enter the Y Coordinates:");
    yaxis = sc.next();
    
    if(xaxis.length()<2 && yaxis.length()<2)
    {
    	here you have to convert string to int and compare them last write what u want..
    }
    }
    }

  9. #9
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: Basic java problem


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured