Doubt with scanner

Asked

Viewed 63 times

0

I wanted a help with a program that would ask the person what city they’re from. If she typed a city other than the programmed one, the program would say that the person could not proceed (I intend to use If for this).

Scanner teste = new Scanner(System.in);
System.out.println("Digite sua cidade: ");

After that, I would have to create a variable called citypedigitated, and later the citypedigitated IF is different from the programmed city,.

The problem is that I don’t know which primitive variable to use for the city (if it was a number, I could use int or double, for example,but since it’s a text,I don’t know which variable to use).

  • 1

    Why not string? If you use numbers, you will need to map cities in order to be able to relate them. Better to do it directly with strings.

1 answer

1


It uses a type of data that is not primitive, called String.

Note that to compare two strings do not use the == and yes the equals(). Example:

if (stringDigitada.equals("São Paulo")) {
    ...
}

You should look up a tutorial on Java. Or on second thought, I don’t know why you’re learning basic programming with Java, whether it’s college stuff or something. I think it would be better to start with a structured language and not an object-oriented language. Java is object-oriented, which makes things more complicated, for example a String is an object, which is a type of data that has functions attached to it (note in the above example that the object stringDigitada has a function equals() added to it, which is being called so: stringDigitada.equals(...)). These functions are called "methods".

If you want to learn programming and don’t have a special reason to use Java, I suggest that you first choose a language that supports structured programming such as Python or C and then look for a suitable learning material to guide you along this path, that can be a tutorial, book or video lessons. Maybe even before doing so you will look for material to learn about something called "programming logic", which will probably be taught in some pseudo language like Portugol (although it is possible to find material on programming logic already in the desired programming language).

I hope that these guidelines can be useful.

  • I don’t have a special reason to learn, I’m simply an autodidact. Maybe I’ll identify myself in the area and bring it to my life, considering I’m pretty new. I took an advanced excel course and was my first contact with programming,I liked and I’m trying to delve into it simply for interest.Incidentally, I’m learning through some video lessons from a guy named "Lucas Iório" on youtube. He published a course on "Java for beginners". In class 5, he talks about scanners, but the impossibility of doing what I wanted hit me the question and I came here to ask

  • Good, I hope you like it. What I said in the answer is the "path of stones" to try to facilitate your learning and so you are more interested, although the area has many challenges, but they are challenges to solve. I hope it can be useful.

  • I took a look here, the "teacher" only talks about the "strings" in class 9...Considering that I’m in 5, I still have a good way to go, but I don’t intend to skip classes,...

  • Ok. If you think the answer answered your question, don’t forget to accept it by clicking on the top left corner of it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.