జావాలో తదుపరి Int() విధానం ఏమిటి?
ఉదాహరణ 1
మన మొదటి డైవ్ను ప్రాథమిక ఉదాహరణకి తీసుకుందాం.import java.util.Scanner;
public class TestIntInput {
public static void checkInt(String testData) {
System.out.println(testData);
Scanner scanner = new Scanner(testData);
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
// calling the nextInt() method
System.out.println(scanner.nextInt() + "\t\t INT FOUND!");
} else {
System.out.println(scanner.next() + "\t\t");
}
}
scanner.close();
System.out.println();
}
public static void main(String[] args) {
String testData1 = "My 3 years old cat Diana, just gave birth to 5 healthy babies.";
String testData2 = "The number 1 place to learn Java is CodeGym!";
String testData3 = "6; 5 4 3. 2 1 !";
checkInt(testData1);
checkInt(testData2);
checkInt(testData3);
}
}
అవుట్పుట్
నా 3 సంవత్సరాల పిల్లి డయానా, కేవలం 5 ఆరోగ్యకరమైన పిల్లలకు జన్మనిచ్చింది. నా 3 INT దొరికింది! ఏళ్ల పిల్లి డయానా, ఇప్పుడే జన్మనిచ్చింది 5 INT FOUND! ఆరోగ్యకరమైన పిల్లలు. జావా నేర్చుకోవడానికి నంబర్ 1 ప్లేస్ కోడ్జిమ్! సంఖ్య 1 INT కనుగొనబడింది! జావా నేర్చుకునే ప్రదేశం కోడ్జిమ్! 6; 5 4 3. 2 1 ! 6; 5 INT కనుగొనబడింది! 4 INT కనుగొనబడింది! 3. 2 INT కనుగొనబడింది! 1 INT కనుగొనబడింది! !
వివరణ
testData3 లోని పై ఉదాహరణలో గమనించవలసిన ఒక విషయం ఏమిటంటే, ఒక సంఖ్యను వ్యక్తిగత పూర్ణంగా స్కాన్ చేయడానికి స్థలం-వేరు చేయబడాలి. 6 మరియు 3 పూర్ణాంకాలుగా గుర్తించబడకపోవడానికి కారణం అవి వరుసగా పెద్దప్రేగు మరియు కామాతో వేరు చేయబడినవి.ఉదాహరణ 2
ఈ ఉదాహరణ పూర్ణాంకాలుగా స్కాన్ చేయడానికి సిస్టమ్ ఇన్పుట్ని ఉపయోగిస్తుంది.import java.util.Scanner;
public class TestSystemInput {
public static void getFinalExamScore() {
System.out.println("Get Your Final Exam Score!\n");
int finalScore = 0;
int totalCourses = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter total Courses: ");
totalCourses = scanner.nextInt();
for (int i = 0; i < totalCourses; i++) {
System.out.println("Enter score in course " + (i + 1) + " : ");
finalScore = finalScore + scanner.nextInt();
}
System.out.println("Your final Score = " + finalScore);
scanner.close();
}
public static void main(String[] args) {
getFinalExamScore();
}
}
అవుట్పుట్
మీ చివరి పరీక్ష స్కోర్ పొందండి! మొత్తం కోర్సులను నమోదు చేయండి: 3 కోర్సు 1లో స్కోర్ను నమోదు చేయండి : 10 కోర్సు 2లో స్కోర్ను నమోదు చేయండి : 15 కోర్సు 3లో స్కోర్ను నమోదు చేయండి : 15 మీ చివరి స్కోరు = 40