#Java code won't run on Apache Netbean 17

1 messages · Page 1 of 1 (latest)

peak lava
#

This should be pretty basic. But it wants a ';' in a line that doesnt need another one. It also won't let me run the file when I try. why?

frigid vine
#

could you show the error?

#

also should New be capitalized? i don’t recall the syntax but it doesn’t look right.

green monolith
#

It's new not New

peak lava
#

why does it have to be lowercase? I thought it was a name

green monolith
#

No it's an operator

peak lava
#

oh

frigid vine
#

new is a keyword that instantiates a new object in java - it is case sensitive

peak lava
#

Ok I ran the code and it worked

#

so what was myObj called then? just myObj?

#

i thought each one has a unique identifier i can put with it

frigid vine
#

yes that was the name. you can name it anything

#

i always used Scanner sc = new Scanner(System.in)

#

but some people call it input, scanner, keyboard, etc.

#

it goes [type] [objectName] = new type

#

so in that case, you’re creating a Scanner object with a name of myObj and passing in “System.in” (your keyboard) as a parameter.

peak lava
#

er...right...

frigid vine
#

i'd be happy to try to explain if that didn't make sense - just let me know!

peak lava
#

It's all really complicated rn so

#

idk

#

i think im gonna leave it for now and come back to it tonight

#

try to add on to the code so I can retrieve the date after I enter it as a user

frigid vine
#

okay, totally alright to take a break! this was all really confusing for me when i was learning it.

#

an "object" can be anything. you can make a Person object, Car object, Bed object, etc. in this instance, you're creating a Scanner "object" that is going to allow your program to get information from the user.
Scanner(object) myScanner(name) = new(keyword) Scanner(System.in) In this case, you're passing in the value "System.in" which is the computer's way of saying your keyboard. If you wanted to read from a file, you could put the file in there like this -
Scanner myScanner = new Scanner("file.txt") and it would read information from the file!
The Scanner object is within Java and has some method it allows you to use, such as nextLine() which you used in your program. When you learn about objects and specifically scanners, your programs are able to gain a lot of complexity because you can do so much more with them! You'll learn in no time, don't worry.

peak lava
#

how do I make a second object? I can't also call it myObj, can I?

#

can I change the name of the first object?

#

from myObj to "uname" or "userName"?

frigid vine
#

you can't have 2 objects of the same type with the same name. it's like if you were to create two int variables. you'd have int num1 and int num2 but you cant have two num1s

peak lava
#

i think i have something working

frigid vine
#

and you'd likely not want to name the Scanner something like "userName" because it can do more than that. For instance:

Scanner scanner = new Scanner(System.in);
System.out.println("What's your name?: ");
String name = scanner.nextLine();

// now here, you can actually use the SAME scanner to get more info out of the user.
System.out.println("Hey, " + name + " , how are you today?");
String mood = scanner.nextLine();
#

the same scanner object can be used for a bunch of different things

#

when you do scanner.nextLine(); it basically reads the value in that line and then either stores it in the variable you named, or if you didn't store it, it just reads over it and it's gone. then it'll go on to the next input

peak lava
#

hmm

frigid vine
#

also, using "nextLine()" will read until the user enters an escape sequence (enter) - if you wanted to only read until the a space, you could use "next()"

Scanner scanner = new Scanner(System.in)
System.out.println("What's your name");
// user says "karston johnson"
String name = scanner.next(); // name now equals "karston" 
peak lava
#

strange

frigid vine
#

and if you're asking something like their age and want to make sure they don't enter something crazy like their name, you can do scanner.hasNextInt() to make sure the value they entered is a number before you store it as such

#

there's a bunch of cool stuff!

peak lava
#

how can I code a confirmation after a user input entry?

frigid vine
#

well, that depends on what you're trying to verify

peak lava
#

I want it to ask the time,name, etc, and after you input your name, I want it to say "Type yes to confirm"

#

and "type no to retry"

#

and then if you type yes, it confirms the input and moves on to the next question

#

and if you type no, or any other text other than Yes, it will ask you to re-enter your input for the first questiob

frigid vine
#

if you wanted to re-prompt the user to enter a valid input you could do something like this:

confirmed = false;
while !(confirmed) {
  System.out.println("Do you confirm?");
  if (scanner.next().equalsIgnoreCase("yes")) {
    confirmed = true;
  }
}
#

so while the user hasn't confirmed it yet, it will keep asking them until they say yes

peak lava
#

if they say no will it ask for re-entry?

frigid vine
#

oh, well not with the code i provided. you could add that.

#
confirmed = false;
// assume info was taken somewhere else
while !(confirmed) {
  System.out.println("Do you confirm this? + info ");
  if (scanner.next().equalsIgnoreCase("yes")) {
    confirmed = true;
  } else {
    System.out.println("Please enter your info again");
    info = scanner.nextLine();
  }
}
#

you could do something like that

#

so if yes, continue on, if no, reprompt info and store, then ask for confirmation again

peak lava
#

working on it rn

frigid vine
#

so you want to confirm that their username is valid?

peak lava
#

how do I use " as a character?

#

was it /"?

frigid vine
#

yes

#

"

#

\ <-- use this

#

you don't need multiple scanners

#

you only need 1 scanner

peak lava
#

whaat? why not? i'm grabbing different info

#

it also helps me seperate it into different lines so they go in sequence

frigid vine
#
Scanner sc = new Scanner(System.in)
System.out.println("Enter username: ");
String username = sc.nextLine();

System.out.println("Enter year");
int year = sc.nextInt();
#

you only need 1 scanner and you can use that scanner to get multiple pieces of info

peak lava
#

huh...weird

#

i'll try that when i finish the confirmation code

frigid vine
#

think of it like this:

the scanner's job is to absorb all pieces of information the user enters. you can create different VARIABLES to store that information.

peak lava
#

@frigid vine my code ide doesnt like "confirmed"

#

cannot find symbol/variable

frigid vine
#

you can use any word you'd like, that was just an example

#

oh lol, you prob didnt put boolean in front of it

peak lava
#

how do i make it not have an error?

frigid vine
#

i left that out on accident

#

so it'd be
boolean confirmed = false

#

or something of that sort

peak lava
#

got it

#

one more error i think

#

it doesnt like the while !(confirmed) { line

frigid vine
#

you mind sharing the code?

peak lava
frigid vine
#

oh, you don't have a closing bracket.

#
while !(confirmed) {
    // your code here
} <-- you need this
peak lava
#

it just seems to generate more errors

green monolith
frigid vine
#

you're also missing brackets on your if statement. i'd also recommend checking out a quick tutorial

peak lava
#

I went thru all the tutorials on w3s schools

#

i think the best way I can learn is just hands on like the way im doing it now

#

droning thru tutorials just makes me turn the computer off and go find something else to do

green monolith
#

I feel like you skipped some steps, like practicing each new concepts

#

When going through a tutorial you shoud be sure you understood and know how to apply the new concept before passing to the next one

peak lava
#

im kind of in the process of practicing the concepts rn

#

this is how i learned javascript

#

and html and css

#

starting a project and learning the syntax and stuff on the way

#

its easier to learn when i'm applying my knowledge to something important to me immediately

green monolith
#

You do whatever works for you, I was just saying because you seem to struggle way to much for what you were doing. But anyway, we all learn differently so good luck!

peak lava
#

that mistake with the "new" piece was pretty basic but the nice part about it is now i understand it

#

since i made the mistake already

peak lava
#

@frigid vine it was the !

#

it's supposed to be inside the brackets

#

not outside

#

everything else was fine

#

so my new problem is that the confirmation code doesn't let you re-enter the input for the username

#

it's too busy trying to get you to say "yes" and when you don't, instead of letting you back off and try re-entering the name, it just keeps trying to get a yes from you