|
-
August 20th, 2009, 08:21 AM
#1
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");
}
}
-
August 20th, 2009, 11:28 AM
#2
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?
-
August 20th, 2009, 11:56 AM
#3
Re: Basic java problem
 Originally Posted by Londbrok
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
-
August 20th, 2009, 11:58 AM
#4
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!
-
August 20th, 2009, 12:04 PM
#5
Re: Basic java problem
 Originally Posted by Xeel
Code:
int strlength = str.length();
It gives me this error
P1.java:21: int cannot be dereferenced
int strlength = xaxis.length();
^
1 error
-
August 20th, 2009, 12:13 PM
#6
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");
-
August 20th, 2009, 12:22 PM
#7
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
....
-
August 20th, 2009, 12:48 PM
#8
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..
}
}
}
-
August 20th, 2009, 12:51 PM
#9
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|