#Java code won't run on Apache Netbean 17
1 messages · Page 1 of 1 (latest)
could you show the error?
also should New be capitalized? i don’t recall the syntax but it doesn’t look right.
It's new not New
why does it have to be lowercase? I thought it was a name
No it's an operator
oh
new is a keyword that instantiates a new object in java - it is case sensitive
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
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.
er...right...
i'd be happy to try to explain if that didn't make sense - just let me know!
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
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.
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"?
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
i think i have something working
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
hmm
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"
strange
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!
how can I code a confirmation after a user input entry?
well, that depends on what you're trying to verify
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
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
if they say no will it ask for re-entry?
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
so you want to confirm that their username is valid?
whaat? why not? i'm grabbing different info
it also helps me seperate it into different lines so they go in sequence
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
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.
you can use any word you'd like, that was just an example
oh lol, you prob didnt put boolean in front of it
how do i make it not have an error?
i left that out on accident
so it'd be
boolean confirmed = false
or something of that sort
you mind sharing the code?
oh, you don't have a closing bracket.
while !(confirmed) {
// your code here
} <-- you need this
it just seems to generate more errors
Maybe you should go through a simple tutorial.
https://www.tutorialspoint.com/java/java_basic_syntax.htm
you're also missing brackets on your if statement. i'd also recommend checking out a quick tutorial
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
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
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
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!
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
@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