Hey guys,

I recently wrote this program and it compiles and works but I have two issues that need addressing and I'm a bit confused on how to solve them.

Question 1)

System.out.println("What's your name?");
        name = Scan.nextLine();
        
        System.out.println("How old are you?");
        age = Scan.nextInt();

I always thought Scan.next and so had to start with a lowercase s. It does not work with a lower case s (works with a capital S though), I get this error when I do use it:

Error:

AgeStatus.java:18: cannot find symbol
symbol : variable scan
location: class AgeStatus
name = scan.nextLine();
^
1 error

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.

(the program still runs but I'd like to learn why I still got a compile error and I'm a newb programmer so any help is appreciated :P)


and my second question is: How can I put everything on one line? when I run the program it outputs as this:

----jGRASP exec: java AgeStatus
What's your name?
Dude
How old are you?
26
Dude is 26 years old and is
young.

----jGRASP: operation complete.

I'd like my program to read out as:

Dude is 26 years old and is young.


Here is my program:

// AgeStatus.java


import java.util.Scanner;

public class AgeStatus
{
    // Prompts for name and age
    
    public static void main(String[] args)
    {    
        Scanner Scan = new Scanner (System.in);
        
        String name;
        int age;
        
        System.out.println("What's your name?");
        name = Scan.nextLine();
        
        System.out.println("How old are you?");
        age = Scan.nextInt();
        
        System.out.println(name+" is "+age+" years old and is");

            
                if(age < 30)
                {    
                    System.out.println("young.");
                }    
                else if( age >= 30 && age < 60)
                {    
                    System.out.println("middle-aged.");
                }    
                        else if( age > 60)
                            
                            {
                                System.out.println("old!");
                            }
                            
    }
}

Thanks in advance!

Edited 6 Years Ago by Wootens: n/a

The case of the letter doesn't matter. What matters is that the spelling of the definition of a variable must match its usage:

Scanner scan = new Scanner (System.in);  // Define a variable
  name = scan.nextLine();  // use the variable

How can I put everything on one line?

The println() method moves to the next line after printing.
Use the print() method to keep the output on the same line

When you press Enter the input cursor will go to the next line.

Edited 6 Years Ago by NormR1: n/a

Weird. I've changed to print and it still goes to the next line.

import java.util.Scanner;

public class AgeStatus
{
    // Prompts for name and age
    
    public static void main(String[] args)
    {    
        Scanner Scan = new Scanner (System.in);
        
        String name;
        int age;
        
        System.out.println("What's your name?");
        name = scan.nextLine();
        
        System.out.println("How old are you?");
        age = scan.nextInt();
        
        System.out.print(name+" is "+age+" years old and is");

            
                if(age < 30)
                {    
                    System.out.print(" young.");
                }    
                else if( age >= 30 && age < 60)
                {    
                    System.out.print(" middle-aged.");
                }    
                        else if( age > 60)
                            
                            {
                                System.out.print(" old!");
                            }
                            
    }
}

Edited 6 Years Ago by Wootens: n/a

it still goes to the next line.

Did you read my comment:
When you press Enter the input cursor will go to the next line.
Is this what you are talking about?

Does the code you just posted compile OK? I don't see a definition for the variable: scan
There is a definition for: Scan

Edited 6 Years Ago by NormR1: n/a

Did you read my comment:
When you press Enter the input cursor will go to the next line.
Is this what you are talking about?

No not really.

The final output is

Dude is 19 years old and is
young.

I'm trying to get it all on one line so that the final output is

Dude is 19 years old and is young.

Would you post the actual code you are using. What you posted doesn't compile.

The code doesn't compile, I get 2 errors since scan is not uppercase, the program runs though.

import java.util.Scanner;

public class AgeStatus
{
    // Prompts for name and age
    
    public static void main(String[] args)
    {    
        Scanner Scan = new Scanner (System.in);
        
        String name;
        int age;
        
        System.out.println("What's your name?");
        name = scan.nextLine();
        
        System.out.println("How old are you?");
        age = scan.nextInt();
        
        System.out.print(name+" is "+age+" years old and is");

            
                if(age < 30)
                {    
                    System.out.print(" young.");
                }    
                else if( age >= 30 && age < 60)
                {    
                    System.out.print(" middle-aged.");
                }    
                        else if( age > 60)
                            
                            {
                                System.out.print(" old!");
                            }
                            
    }
}

code doesn't compile, I get 2 errors since scan is not uppercase, the program runs though.

But NOT the program that is shown in the source. You are executing an OLD version
Delete the AgeStatus.class file and try it again.

Simple fix to your problem of it printing the end result in 2 lines. Inside each if statement, just print the whole line.

Although, for me, it prints exactly as you wanted it to print using your code.

Edited 6 Years Ago by Akill10: n/a

Simple fix to your problem of it printing the end result in 2 lines. Inside each if statement, just print the whole line.

Although, for me, it prints exactly as you wanted it to print using your code.

Cheers you guys!

Works perfectly now :)

This question has already been answered. Start a new discussion instead.