Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


jasmeet
Scanner
3 Answer(s)      a year and 4 months ago
Posted in : Java Beginners


Hi, I am facing a problem in the following code as what should be the while condition I should use in order to get the loop keep running until i get the desired double value and come out of the loop once condition is met.

            System.out.println(" You have selected Celsius");
    temp_name= "Celsius";

    do
    {
    Scanner temp_ip= new Scanner(System.in);
    System.out.println("Please enter the temperature in celsius");


    if (temp_ip.hasNextDouble())
    {
        temp_usr= temp_ip.nextDouble();
    }
    else
    {
        System.out.println("Invalid Input. Try Again!!!");
    }
    }
    while();

Advertisement
View Answers

August 27, 2013 at 6:43 PM


I think it should help you jasmeet

package myFunc;

import java.util.Scanner;

public class ScannerDemo { static double n=0.0; public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in);

     while(!isDouble(sc.nextLine())){
         System.out.println("Invalid Input. Try Again!!!");
     }
}
static boolean isDouble(String str) {
    try {
        n=Double.parseDouble(str);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}

}



August 27, 2013 at 6:43 PM


I think it should help you jasmeet

package myFunc;

import java.util.Scanner;

public class ScannerDemo { static double n=0.0; public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in);

     while(!isDouble(sc.nextLine())){
         System.out.println("Invalid Input. Try Again!!!");
     }
}
static boolean isDouble(String str) {
    try {
        n=Double.parseDouble(str);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}

}



August 28, 2013 at 7:02 AM


hello, thanks for the answers. I know that it could be done using try n catch concept but I don't want to use this and want to execute without initially just to have a good hold on basics.

Please post your answer without using try n catch.

Thanks again! jasmeet



Related Tutorials/Questions & Answers:
Scanner
Scanner   Hi, I am facing a problem in the following code as what should be the while condition I should use in order to get the loop keep running...= "Celsius"; do { Scanner temp_ip= new Scanner(System.in
Scanner class
Scanner class  what have to do when an error occur about Scanner class.i code scanner sc=new Scanner(System.in); but it shows an error regarding this.   Use Scanner sc=new Scanner(System.in
Advertisements
Scanner class
Scanner class  import java.util.*; class Dd { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int... Scanner class was introduced in java 5
scanner program
scanner program  a news paper boy purchase 100 papers every day.he purchases papers at Rs.1.50 and sells at Rs.2.00 at each.any unsold news papers... his profit..using scanner
without scanner
and class and didn't use the scanner .   Hi Friend, Whether
scanner - Java Beginners
scanner   how to ask user to enter index in arry using scanner
scanner problem - Java Beginners
scanner problem  the program that enters sides of the triangle using scanner and outputs the area of triangle  Hi Friend, We... main(String []args){ Scanner scanner = new Scanner(System.in
Hp scanner - Java Beginners
Hp scanner  Hi guys, i would like to access hp scanner using java programme. could you refer me some useful information to proceed
scanner Class Error - Java Beginners
scanner Class Error  Hello Sir ,When i run the program of Scanner Class ,there is Error Can not Resolve Symbol-Scanner how i can solve..., Scanner class is not provided. Check your version.It should work with java 1.5
XII STD RECURSION WITHOUT SCANNER
not wish to use the scanner class Is there anything i can do to improve
Java file scanner
Java file scanner In this section, you will learn how to read a file using Scanner class. Description of code: J2SE5.0 provides some additional classes.... It also parses the primitive data. Scanner class gives a great deal of power
barcode scanner device usage in database application
barcode scanner device usage in database application  Am trying to design a supermarket inventory desktop database application however I can't seem to know how to use a barcode scanner device on my application can anyone put me
Pattern,Matcher,Formatter and Scanner classes
Pattern,Matcher,Formatter and Scanner classes This section Describes : 1... using the Formatter and Scanner classes and the PrintWriter.format/printf..., n) Formatted input The scanner API provides basic input
with out using scanner mul two matrices
with out using scanner mul two matrices  write a java program to multiply two matrices and print there result. note: don't use the scanner and the values is passed at run time....   import java.io.*; class
input using scanner and add/sub/mul/div of a matrix
input using scanner and add/sub/mul/div of a matrix  hiiii...... performe matrix add/sub/mul/div using scanner
Creating a File I/O using import scanner, io.* and text.*
Creating a File I/O using import scanner, io.* and text.*  open a file for reading and open another file for writing data. input file hours-int, hourly rate-double, name-string. output file name-string, hours-int, hourly rate
can we use scanner class,class , object and methods to get output without using constructor ????
can we use scanner class,class , object and methods to get output without using constructor ????  im getting error here..i hav used scanner class... am i getting error here... can we get output using scanner class , object
Local Port Scanner
Local Port Scanner       This is a simple program of java network. Here, we are going to define class named "LocalPortScanner" in which we use ServerSocket class
Low port Scanner
Low port Scanner       In this section, you will learn how to get local port number of the local machine. Here, we define the name of program LowPortScanner
How to read from the console
Description: Scanner was introduced in jdk 1.5. This class help in taking the input from the console. Here in this sample program it will take one word...;sc = new Scanner(cons.reader());     
How to Read file line by line in Java program
in Java using Scanner Class: Besides the Buffer reader, using Scanner class.... The benefit of using Scanner class is that it has more utilities like nextInt... line by line using Scanner Class: package ReadFile; import
Java read text file
; + e.getMessage()); } } } The other way to Read file line by line is by using Scanner Class. The benefit of using Scanner class is that we do not need to convert... hasNextLine() method to read the contents of a file line by line. Scanner
SimScan
SimScan       SimScan (Similarity Scanner) is a utility we have developed internally to find duplicated or similar fragments of code in large Java source
Java char to string
is using Scanner class. In this section we have also used BufferReader to convert...;. In Scanner class example, we have used toString() method, which reads a character.../user at the run time. Scanner class then reads the input from System.in
Swapping of two numbers in java
have used input.nextInt() method of Scanner class for getting the integer type...:"); Scanner in = new Scanner(System.in); a = in.nextInt
java query
java query  why do we use scanner class
Header file
Header file  when we use, Scanner input=new Scanner(System.in); this statement in a java program,which header filemust be imported to the program?   Hi Friend, Import the package java.util.*; Thanks
student details
student details  create an application for details of 1st to 5th standard students by using loops and scanner
Java User Input
Java User Input  I am using Scanner class for user Input. Scanner s = new Scanner(System.in); String name = s.next(); but i am unable to store full... nextLine() method of Scanner class
 

 

 

DMCA.com