Results 1 to 9 of 9
- 02-28-2011, 03:41 AM #1
Member
- Join Date
- Jan 2011
- Location
- Park Ridge, IL
- Posts
- 10
- Rep Power
- 0
- 03-01-2011, 02:01 AM #2
All input from the terminal/gui is as a String. So, you could read the string and cast/parse to convert to a different data type.
- 03-02-2011, 01:53 AM #3
Member
- Join Date
- Jan 2011
- Location
- Park Ridge, IL
- Posts
- 10
- Rep Power
- 0
Even from a command window?
- 03-02-2011, 02:02 AM #4
Yes.
If you use a Scanner and the nextInt method, it just hides that fact that it reads a String and calls the Integer.parseInt method.
- 03-02-2011, 03:16 AM #5
Member
- Join Date
- Jan 2011
- Location
- Park Ridge, IL
- Posts
- 10
- Rep Power
- 0
Here is the code -
import java.util.Scanner;
public class W1
{
public static void main(String[] args)
{
int number;
Scanner keyboard = new Scanner(System.in);
System.out.println("enter a number or \"n\" to end");
number = keyboard.nextInt();
while (number != 'n')
{
System.out.println("hello");
System.out.println("enter a number or 'n' to end");
number = keyboard.nextInt();
}
System.out.println("bye");
}
}
If I put in a number, the loop works fine. When I put in anything other than a number, it just crashes.
- 03-02-2011, 03:20 AM #6
Because n is not an int so if you enter that the nextInt method will throw an exception. The advice given above may not have been clear. Get user input as a String. Check if that String is "n". If it isn't convert String to int.
- 03-02-2011, 04:10 AM #7
Member
- Join Date
- Jan 2011
- Location
- Park Ridge, IL
- Posts
- 10
- Rep Power
- 0
Now, it just loops forever, even if you enter n
import java.util.Scanner;
public class W1
{
public static void main(String[] args)
{
String number;
String input;
Scanner keyboard = new Scanner(System.in);
System.out.println("enter a number or \"n\" to end");
number = keyboard.nextLine();
while (number != "n")
{
System.out.println("hello");
System.out.println("enter a number or 'n' to end");
number = keyboard.nextLine();
}
System.out.println("bye");
}
}
- 03-02-2011, 04:31 AM #8
Do not compare Strings or objects using == or !=, use the equals method instead.
Java Code:if(str1.equals(str2)) if( ! str1.equals(str2))
- 03-02-2011, 11:42 PM #9
Member
- Join Date
- Jan 2011
- Location
- Park Ridge, IL
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
problem with argument list and precedence "(" and ")"
By helpisontheway in forum Advanced JavaReplies: 6Last Post: 12-24-2009, 07:50 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks