Question

In: Computer Science

Hi I am quite confused with how to use scanner for my java project? Can someone...

Hi I am quite confused with how to use scanner for my java project? Can someone help me with this? Thanks

Solutions

Expert Solution

/* BELOW DOWN IS SIMPLE PROGRAM WHICH USE SCANNER CLASS TO INPUT STRING AND INTEGER */

import java.util.Scanner; // FIRST IMPORT THIS CLASS

public class Example {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // DECLARE ITS OBJECT
System.out.print("Enter name: ");
String name = sc.nextLine(); // FOR STRING INPUT USE .nextLine() method
System.out.print("Enter first Integer: ");
sc = new Scanner(System.in); // to clear buffer
int a = sc.nextInt(); // for integer input use .nextInt() input
System.out.print("Enter second Integer: ");
sc = new Scanner(System.in);
int b = sc.nextInt();
System.out.println("Sum is: "+ (a+b));
}
}

/* OUTPUT*/

/* TYPE OF INPUT YOU WANT YOU CAN CALL METHOD FROM SCANNER CLASS

FOR EG: TO TAKE CHARACTER INPUT USE .next() */'

/* THIS IS LIST OF VARIOUS METHOD */

import java.util.Scanner;
import java.util.regex.Pattern;

public class Example {

   public static void main(String[] args) {

       // Initialize Scanner object
       Scanner scan = new Scanner("Vince1;Gandhi;Albert");
       // declare the delimiter to be used by Scanner object
       scan.useDelimiter(";");

       /*Initialize the String pattern which
       signifies that the String token contains
       characters of the alphabet only*/
       Pattern pattern = Pattern.compile("[A-Za-z]*");

       while(scan.hasNext()){
           // check if the token consists of declared pattern
           if(scan.hasNext(pattern)){
               System.out.println(scan.next());
           }
           else
               scan.next();
       }

       // closing the scanner stream
       scan.close();

   }

}

/* YOU NEED PATTERN CLASS TO MATCH REGEX */


Related Solutions

Hi I am working on my chemistry homework about acids and bases and I am confused...
Hi I am working on my chemistry homework about acids and bases and I am confused on this question from a lab. We used grape juice and cranberry juice and we had to put HCl and NaOH in the test tube full of juice. 1. Referring back to procedure 1 of the experiment, which of the two juices is more useful as a general pH indicator and why? I think it is grape juice but I just don't know what...
Hi I am working in an accouting project for a taco food truck. Can someone please...
Hi I am working in an accouting project for a taco food truck. Can someone please help me find the variable and fixed costs or such ? i can provide more info if needed. please assists. I have the costs of opening a start up food truck but that is it . thanks. what info do you need from me if any
Hi I am getting error in implement some test case using Java. I am adding my...
Hi I am getting error in implement some test case using Java. I am adding my code and relevant files here, and the failed test. Please let me know where my problem is occuring and fix my code. Thanks! I will upvote. Implement a class to perform windowing of a Hounsfield value Implement the class described by this API. A partial implementation is provided for you in the eclipse project; however, unlike the previous class, very little work has been...
Hi, Working on a project in my group for class and I am having some issues...
Hi, Working on a project in my group for class and I am having some issues My part is current state of the business. It is a store and the annual sales are $460,000 Other info I have is: Ownership and Compensation; Percent Ownership Personal Investment Mitchell George, Founder & CEO 25% $125,000Katie Beauseigneur, COO 15% $75,000 Melissa Dunnells, CFO15% $75,000 Also, a medium coffee price from store is $3.75 Sarah Griffin, Marketing Officer 10% $50,000 Katharina Ferry, HR Director10%...
Hi, I am struggling to understand this worksheet my professor gave us for practice. Could someone...
Hi, I am struggling to understand this worksheet my professor gave us for practice. Could someone make any sense of this? The scenario: Sodium is found largely in the extracellular compartment with concentrations between 130-145 mM with intracellular sodium concentrations between 3.5-5mM. This chemical difference gives sodium a large concentration gradient, which when permitted (by opening of a channel or through facilitated transport) will move down its concentration gradient to enter the cell. Sodium also has a favorable electrical gradient;...
This is my lab and I am very confused regarding the calculations! It is supposes to...
This is my lab and I am very confused regarding the calculations! It is supposes to demonstrate Le Chatelier’s principle. After doing the following I need to calculate the molar absorbance for Co(H20)6 2+. The path length is 1cm and absorbance is .551. I got 5.51 L/mol *cm. I also need to calculate the CoCl4 2- molar absorbance which I do not understand. The absorbance is .570 and the path length is 1cm. I do not know how to find...
Can someone please explain to me the Hobby Loss Rule Sec. 183? I am still confused...
Can someone please explain to me the Hobby Loss Rule Sec. 183? I am still confused about it. It would also help if an example was provided with the explanation.
Best Forecasting Model Hi, I am trying to find the best suitable model for my project...
Best Forecasting Model Hi, I am trying to find the best suitable model for my project that I should use for forecasting the values. Following are the models that I used and their respected values. Model RMSE MAE MAPE Holt's 201.616 130.072 2.054 Holt's (Log Transform) 197.723 125.330 1.971 Damped Trent .    206.040 128.525 2.028
Hi! Can someone summarize what the reaction article is saying? I am having trouble understanding it....
Hi! Can someone summarize what the reaction article is saying? I am having trouble understanding it. The purpose of this experiment was to investigate the catalytic performance of solid acid catalyst developed from modification of coal combustion fly ash to synthesize methyl 4-aminobenzoate (MAB) by Fischer esterification between 4-aminobenzoic acid and methanol. Upon acid modification of fly ash, surface acidity of solid acid catalyst (SAC) is greatly enhanced as a result of increased surface area, augmented silica content, and fabricated...
Hi I am confused a little about equations for constructive and destructive interference and also about...
Hi I am confused a little about equations for constructive and destructive interference and also about the difference in equations for diffraction and interference and also what equations we use for young's single slit experiment and double slit experiment.Can you please explain the difference??
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT