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, 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 not sure how to go about answering this question on my homework. ABC...
Hi I am not sure how to go about answering this question on my homework. ABC corporation adopts a compensatory stock option plan. the plan covers 25 executives and provides each executive 300 otions to purchase stock for $100/share. the options vest in 3 years. the plan starts on 1/1/18 and matures 12/31/20. we assume a 95% retention rate. at 12/31/20 22 executives earn the options. each option is valued at $17/share. record the journal entries for 12/31/18, 12/31/19, and...
This is to be done in excel (which I can do), but I am confused about...
This is to be done in excel (which I can do), but I am confused about which tail test to use and why to accept or reject H0- and the summary. According to research, holiday shoppers spent an average of $370 over a holiday weekend in 2008. The accompanying data show the amount spent by a random sample of holiday shoppers during the same weekend in 2009. Retailers were concerned that the economic downturn was affected holiday sales. Does this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT