J'ai un problème à essayer de comprendre comment je peux parcourir une ligne de texte d'entrée au clavier que l'utilisateur donnera ex:
Anika 14 Dan 16
Je veux lire chaque jeton et attribuer à String name, Int, age, String name, int age. dans cet ordre. C'est facile cependant, si l'utilisateur entre Anika Anika Anika Anika 13 13 13 Dan 16
Ensuite, je veux que le programme:
Anika,
Integer needed got String,
Integer needed got String,
Integer needed got String,
13,
String needed got Integer,
String needed got Integer,
Dan,
16
Ainsi, le premier sera toujours une chaîne qui est un mot EDIT: "mot", le second une chaîne int et une chaîne thrid qui est un "mot" et le quatrième int. Cependant, je ne peux pas simuler cela.
Scanner scan = new Scanner(System.in);
String name = null;
int age = 0;
String name2= null;
int age2= 0;
if(scan.hasNext() == true)
name = scan.next();
age = scan.nextInt();
name2= scan.next();
age2= scan.nextInt();
Je sais que si je fais le haut, j'obtiens le bon ordre, mais ce sont les entrées supplémentaires que je voudrais ignorer, mais écrire une expression de déclaration pourquoi c'est faux, puis continuer à rechercher le prochain int, ou troisième chaîne et ainsi de suite.
boolean isstring = false;
boolean isnumber = false;
do {
if (scan.hasNext())
{
name = scan.next();
isstring = true;
}
else {
System.out.println("Need String got Integer");
isstring = false;
scan.next();
}
} while (!isstring);
do {
if (scan.hasNextInt())
{
age = scan.nextInt();
isnumber=true;
}
else {
System.out.println("Need Integer got String");
isnumber=false;
scan.nextInt();
}
} while (!isnumber);
do {
if (scan.hasNext())
{
name2 = scan.next();
isstring = true;
}
else {
System.out.println("Need String got Integer");
isstring = false;
scan.next();
}
} while (!isstring);
do {
if (scan.hasNextInt())
{
age2 = scan.nextInt();
isnumber = true;
}
else {
System.out.println("Need Integer got String");
isnumber=false;
scan.nextInt();
}
} while (!isnumber);
}
J'ai essayé d'utiliser faire avec des ifs et cela n'a pas fonctionné. Ma logique est erronée quelque part, et je pense que ce pourrait être la méthode has.next ().
Toute aide serait appréciée!!
Si l'entrée est un mot en attendant un Integer, elle lèvera InputMismatchException. nextInt()
commencez par lire la valeur sous forme de chaîne puis analysez-la comme un entier, donc si vous ignorez la valeur à l'aide de nextInt, si la valeur est un mot, elle trowa l'exception susmentionnée.
Les changements devraient être:
scan.next()
scan.hasNextInt()
), pas si est une chaîne, car tout entier peut être exprimé sous forme de chaîne. boolean isstring = false;
boolean isnumber = false;
do {
if (!scan.hasNextInt()) {
isstring = true;
name = scan.next();
} else {
isstring = false;
System.out.println("Need String got Integer");
scan.next();
}
} while (!isstring);
do {
if (scan.hasNextInt()) {
isnumber = true;
age = scan.nextInt();
} else {
isnumber = false;
System.out.println("Need Integer got String");
scan.next();
}
} while (!isnumber);
do {
if (!scan.hasNextInt()) {
isstring = true;
name2 = scan.next();
} else {
isstring = false;
System.out.println("Need String got Integer");
scan.next();
}
} while (!isstring);
do {
if (scan.hasNextInt()) {
isnumber = true;
age2 = scan.nextInt();
} else {
isnumber = false;
System.out.println("Need Integer got String");
scan.next();
}
} while (!isnumber);
Une solution naïve utilisant try / catch peut être la suivante
public static void main (String[]args)
{
String name = null;
String name2 = null;
Integer age = null;
Integer age2 = null;
Scanner scan = new Scanner(System.in);
while (scan.hasNext())
{
try
{
if (name == null)
{
System.out.println("Please provide name: ");
name = getNameOrFail(scan);
System.out.println("Name set: " + name);
}
if (age == null)
{
System.out.println("Please provide age: ");
age = getAgeOrFail(scan);
System.out.println("Age set: " + age);
}
if (name2 == null)
{
System.out.println("Please provide name2: ");
name2 = getNameOrFail(scan);
System.out.println("Name2 set: " + name2);
}
if (age2 == null)
{
System.out.println ("Please provide age2: ");
age2 = getAgeOrFail (scan);
System.out.println ("Age2 set: " + age2);
}
}
catch (Exception e)
{
System.out.println(e.getMessage ()); // Print the message put int Exception(message) constructor
scan.nextLine(); // Flush the Scanner cache
}
}
}
public static String getNameOrFail(Scanner scan) throws Exception
{
if (scan.hasNextInt())
throw new Exception("Need String got Integer");
return scan.next();
}
public static Integer getAgeOrFail(Scanner scan) throws Exception
{
if (!scan.hasNextInt())
throw new Exception("Need Integer got String");
return scan.nextInt();
}
Faites attention à la scan.newLine()
clause in the catch, cela est nécessaire car le Scanner utilise un cache avec la dernière entrée, donc si elle n'est pas relue, vous entrez dans une condition de boucle infinie.
Bonne chance!
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots