Tech Support Guy banner
Status
Not open for further replies.
1 - 3 of 3 Posts

·
Registered
Joined
·
41 Posts
Discussion Starter · #1 ·
I've been trying to make a java program, but i'm unable to make one section of it work the way I want it to

It's based on the following attachment. It is supposed to be a random name genrator for an assignment at Uni. It should take the first letter of your first name, the whole of your second name then give it a random number.

I have been able to make it do the last two tasks, however, I do not know how to make it take the first Char from the first name. I've made it take your whole first name because it is the only method that will work.
/**
* @(#)StarWarsNameGenerator.java
*
* StarWarsNameGenerator application
*
* @author
* @version 1.00 2008/3/12
*/

import java.util.*;

public class StarWarsNameGenerator {
static Scanner wars = new Scanner(System.in);

public static void main(String[] args)
{

// Prompt for User's Name
String letter1,letter2;

System.out.println("Please state your First Name");
letter1= wars.charAt(0);

System.out.println("Please state your Surname");
letter2=wars.next();

//determine star wars name
Random generator = new Random();
int num1;
String Starwarsname;

num1 = generator.nextInt(99) + 10;
Starwarsname=letter1+letter2+(num1);

System.out.println("In a galaxy far, far away you are known as " + Starwarsname + " MAY THE FORCE be with you!");
}
}

I believe I need to enter something like "letter1=wars.charAt(0)" but it keeps on coming up with a variety of error messages.

Plese help me, or push me in the right direction.

Regards
 

·
Registered
Joined
·
6,649 Posts
Hi cats31,

The object wars is a Scanner instance and therefore, it has no charAt method.
You may replace the line :
letter1= wars.charAt(0);
by
letter1= wars.next().charAt(0);
 
1 - 3 of 3 Posts
Status
Not open for further replies.
Top