Scanner Key
Scanner Key

Java code enter key with Scanner issues?
public static final int NUMITEMS = 15;
public static void converter(double x)
{
double y = (5.0/9.0) * (x - 32);
System.out.print( "tt" + y + "n");
}
public static void main(String[] args)
{
double[] f = new double[NUMITEMS];
double[] c = new double[NUMITEMS];
Scanner keyboard = new Scanner(System.in);
System.out.print("Fahrenheit" + "t" + "Celsius" + "n");
for(int x = 0; x < 10; x++)
{
System.out.print("#" + (x+1) + ": ");
f[x] = keyboard.nextDouble();
double la = f[x];
converter(la);
}
what happens is the Scanner takes a number input, uses the method converts it to a different number and prints it, but it prints it on the line below f[x]. I need them on the same line. whats wrong with the code? why is the double from converter being printed the line below f[x]?
This is a problem with using the Scanner class for line-oriented input. The token parsing methods just aren't built for that. If "n" is one of the token delimiter characters (as you want it to be so a return at the end of the last token, without trailing spaces, won't be glued to the first token on the next line if there are no leading spaces) then Scanner will skip to the next line for a requested token without any indication that it has done so.
The best fix I know of is to use two scanners...one to get single lines using the nextLine() method, and for each line use a string scanner to get the tokens out of it. Or, you can also use the split() method of String to break the string up into tokens.
![]() |
![]() Snap On Scanner Personalety Key S 20 Brand New US $28.27
|
![]() Snap On Scanner Personality Key S 52 Brand new US $40.85
|
![]() SNAP ON SCANNER S44 MAIN CANBUS PERSONALITY KEY US $78.56
|
![]() SNAP ON TOOL MT2500 MTG2500 SCANNER PERSONALITY KEY SET K 7 K 8 AND K 12 US $18.00
|
![]() Snap On Scanner Personalety Key S 25 Brand New US $31.42
|
![]() Snap On Scanner Personalety Key S 34 Brand New US $15.70
|
![]() OBD2 HONDA Diagnostic Key Scanner FLY100 Locksmith V US $350.00
|
![]() SNAP ON TOOL MT2500 SCANNER PERSONALITY KEY K 5A US $6.30
|
![]() SNAP ON TOOL MT2500 MTG2500 SCANNER PERSONALITY KEY K 7 US $5.85
|
![]() Snap On MT2500 Scanner Personality Key K 12 US $15.00
|
![]() Snap On MT2500 Scanner Personality Key K 8 US $10.00
|
![]() Snap On MT2500 Scanner Personality Key K 7 US $10.00
|
![]() Snap On MT2500 Scanner Personality Key K 5A US $15.00
|
| Powered by phpBay Pro |
Key Features in GFI LANguard Network Security Scanner 8
Leave a Reply
You must be logged in to post a comment.


US $9.95







































