Skip to content
← Back to Community
homework help
Profile icon
SandyyyNwankwo

You will notice that this code allows for interactive input using the Scanner class. Run the program with at least 2 different sets of input, including negative and positive values. Print the output screen for each set of input
int age;
Scanner input = new Scanner(System.in);
System.out.print("Enter age: ");
age = input.nextInt();
while(age <= 0){
System.out.println("Must be positive. ");
System.out.print(" Enter age: ");
age = input.nextInt();
}
System.out.println("You entered " + age);

Voters
Profile icon
EthanTripp
Profile icon
GabrielSousa2
Profile icon
HDH0806A
Profile icon
JosephRay
Profile icon
RiidjeSD
Comments
hotnewtop
Profile icon
PYer

Insteady of pasting the code in, maybe supply the repl link. You also should avoid asking homework questions. It says so on the rules, and it is your job to solve.

Profile icon
Dededecent

hey maybe check the rules before you post it says to avoid asking homework questions just a friendly reminder.

Profile icon
Geocube101

Missing int var type to declare age is supposed to be an int. Should be int age = input.nextInt();

Profile icon
SandyyyNwankwo

@Geocube101 I did. what's next?

Profile icon
Geocube101

@SandyyyNwankwo Do you have the repl? It helps me to interact with the code.

Profile icon
SandyyyNwankwo

@SandyyyNwankwo what will be the else statement since the while statement is less than or equal to zero.

Profile icon
Geocube101

@SandyyyNwankwo I'm sure else statements only work with if and else if

Profile icon
SandyyyNwankwo

@Geocube101 look.
import java.util.Scanner;
class Age{
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int age;
System.out.print("Enter age:");
age = input.nextInt();
while (age <= 0)
{
if (age )
System.out.print("Must be postive");
else
System.out.print("Enter age:");
int age = input.nextInt();
System.out.println("You entered " + age );

}

}

}

Profile icon
Geocube101

@SandyyyNwankwo if the age must be positive then you could just add 1 to the age until it is not positive. If this is the case, then the if statement is not needed. If you are trying to get the user to enter a new age until it is positive, the the if statement shouldn't be needed either. You could just ask the user to input a new age and once age < 0, the while loop will break and the code will continue.

Profile icon
SandyyyNwankwo

@Geocube101 can you write that for me i'm confused

Profile icon
Geocube101

@SandyyyNwankwo

while (age < 0) { System.out.println("Enter your age: ") int age = input.nextInt() } //Whatever Code Comes After This
Profile icon
SandyyyNwankwo

@SandyyyNwankwo like this?
import java.util.Scanner;
class Age{
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int age;
System.out.print("Enter age:");
age = input.nextInt();
(age <= 0)
{
age < 1;
System.out.print("Must be postive");
else
System.out.print("Enter age:");
int age = input.nextInt();
System.out.println("You entered " + age );

}

}

}

Profile icon
Geocube101

@SandyyyNwankwo Is the goal to only accept positive integers?

Profile icon
SandyyyNwankwo
Profile icon
Geocube101

@Geocube101 So you could use loops to ask continually or automatically convert to a positive int if the input is negative. The first method is stated 4 comments ago and the second method is if negative, multiply by '-1'

Profile icon
SandyyyNwankwo

@Geocube101 i'm confused lol. this is my first time

Profile icon
Geocube101
Profile icon
SandyyyNwankwo

@Geocube101 thank you I'm a dummy. quick question how will I do it if it was negative.

Profile icon
Geocube101

@SandyyyNwankwo
if (age < 0)
{
--age = age * -1
}
//Dashes Representative of Spaces

Profile icon
SandyyyNwankwo

@Geocube101 thank you, can you help me with other problems please.

Profile icon
John_WardWard

@SandyyyNwankwo Repl.it is not used to answer homework questions, sorry! @timmy_i_chen

Profile icon
SandyyyNwankwo

@Geocube101 It didn't work for negatives

Profile icon
Geocube101

@SandyyyNwankwo This link has both ways to get only positive ints
https://repl.it/@Geocube101/Ask-For-SandyyyNwankwo

Profile icon
SandyyyNwankwo

@Geocube101 what about negative I got a error

Profile icon
Geocube101

@SandyyyNwankwo What do you mean? I tried the link and it returns only positive ints. Can I see the error?

Profile icon
SandyyyNwankwo

@Geocube101 i'm fine on the positive values.
I need for negative values

Profile icon
Geocube101

@SandyyyNwankwo So you need it to return negatives?

Profile icon
SandyyyNwankwo
Profile icon
Geocube101

@SandyyyNwankwo Switch the < to >

Profile icon
SandyyyNwankwo

@Geocube101 it didn't work. Is it like this
//**********************
import java.util.Scanner;
class Age {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

--age =age*1; while (--age > 0) { --age =age*1; System.out.println("Must be negative. "); System.out.println("Enter your age:"); age = sc.nextInt(); } System.out.println("You entered " + age);

}
}

Profile icon
Geocube101

@SandyyyNwankwo The dashes were only there to represent spaces. Match the indentation of line 5 and 6 with that of line 7 and 8

Profile icon
SandyyyNwankwo

@Geocube101 i'm confused the goal to only accept negative integers?

Profile icon
Geocube101

@SandyyyNwankwo In the code pasted 3 comments ago, the goal is to only pass negative ints.

Profile icon
SandyyyNwankwo

@Geocube101 this if (age < 0)
{
--age = age * -1
?
I got an error

Profile icon
Geocube101

@SandyyyNwankwo Replace the dashes with spaces

Profile icon
Kayne

bad