Doubt with scanner

0

I wanted help with a program that would ask the person whose city it is. If you enter a city other than the one programmed, 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 named civic, and later the civic IF would be different from the programmed city, the user could not proceed.

The problem is that I do not know which primitive variable to use for the civic (if it was a number, I could use int or double, for example, but since it is a text, I do not know which variable to use). >     

asked by anonymous 02.04.2018 / 04:48

1 answer

1

A non-primitive data type, called String , is used.

Note that comparing two strings does not use == , but equals() . Example:

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

You should look for a tutorial on Java. Or better, I do not know why you're learning basic programming with Java, whether it's college stuff or something. I think it would be best 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 data type that has functions added to it (note in the example above that the object stringDigitada has a function equals() aggregated to it, which is being called this: stringDigitada.equals(...) ). These functions are called "methods".

If you want to learn programming and do not 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 suitable learning material to guide you along that path, it can be a tutorial, book or video-lessons. Maybe even before doing this look for material to learn about something called "programming logic", which will probably be taught in some pseudolanguage such as Portugol (although it is possible to find material on programming logic already in the desired programming language). >

I hope these guidelines can be helpful.

    
02.04.2018 / 04:53