3 Replies - 336 Views - Last Post: 09 October 2012 - 07:20 AM Rate Topic: -----

#1 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

charAt printing numbers instead of characters

Posted 09 October 2012 - 07:12 AM

having trouble getting my code to print out the first letter in a string using the charAt method. Here is my code let me know why it is printing out numbers instead of the first character in string source = name1

//* Armando Madrid, Hannah Lewis*//

import java.util.Random;
import java.util.Scanner;

public class Lab5_1
{
  public static void main (String[] args)
  {
    Scanner scan = new Scanner (System.in);
    Random generator = new Random();
   
    String name1, name2, mutation;
   
    System.out.print ("Please enter your first name: ");
    name1 = scan.next();
   
    System.out.print ("Please enter your last name: ");
    name2 = scan.next();
   
    String source = name1;
    char aChar = source.charAt (0);
   
    mutation = name2.substring (0,6);
  
    int num1 = generator.nextInt(10);
    int num2 = generator.nextInt(10);
    int num3 = generator.nextInt(10);
   
    System.out.println (+aChar +mutation +num1 +num2 +num3+"@student.highlands.edu");
  }
}


This post has been edited by macosxnerd101: 09 October 2012 - 07:14 AM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: charAt printing numbers instead of characters

#2 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: charAt printing numbers instead of characters

Posted 09 October 2012 - 07:18 AM

The + before aChar in

System.out.println (+aChar +mutation +num1 +num2 +num3+"@student.highlands.edu");



is converting aChar to a number. Remove it and all will be right in the world.

This post has been edited by rfs02: 09 October 2012 - 07:18 AM

Was This Post Helpful? 1
  • +
  • -

#3 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: charAt printing numbers instead of characters

Posted 09 October 2012 - 07:18 AM

ah thank you very much
Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: charAt printing numbers instead of characters

Posted 09 October 2012 - 07:20 AM

chars can be represented by a letter, or number.
Letters can be turned into ASCII codes (a numerical representation for a letter)
So in your System.out.println()

System.out.println (+aChar +mutation +num1 +num2 +num3+"@student.highlands.edu");


You are doing a math operation. This means it will turn the chars into numbers. The problem is where it says +aChar. If you get rid of the plus it becomes a char added to a String. A non math problem. This will result in char printing out a letter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1