need help with system.out.println....................

Hi, im getting weird results from system.out.println...........

When i run test the result i get its a space then brown like so (with no full stop)

. brown

When i run test2 i get the following

robert

brown

I cant for the life of me understand why this is happening.

Also if i use system.out.print in test i get nothing, both test methods are at the bottom....

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

import java.io.PrintWriter;

publicclass NameFinder

{

publicstaticvoid main (String args[])

{

Names1 play =new Names1();

play.loadFromFile("SURNAMES.txt");

play.loadFromFile1("NAMES.txt");

//display menu to user & process user choice

Scanner input =new Scanner(System.in);

int choice = 0;

while(choice!=4)

{

System.out.println("_");

System.out.println("");

System.out.println("[1] Join the names");

System.out.println("[2] Display names that both name start with the same letter");

System.out.println("[3] Search for names starting with a letter");

System.out.println("[4] Exit");

System.out.println("");

System.out.print("What is your choice? ");

System.out.flush();

//check and process an integer choice

if (input.hasNextInt())

{

choice=input.nextInt();

switch(choice)

{

case 1:

play.test2();

break;

case 2:

break;

case 3:

break;

case 4:

System.out.println("\nGOODBYE!!\n\n");

break;

default:

System.out.println("Please enter a valid menu option (1-4)");

System.out.println("");

break;

}

}

else//choice was not an integer

{

System.out.println("Please enter a valid menu option (1-4)");

input.next();//move on

}

}//end loop

}// end main

}

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

import java.io.IOException;

publicclass Names1

{

private String name;

PrintWriter output =null;

Names1[] LastName =new Names1[2001];//set up array of first names

Names1[] Name =new Names1[2001];;//set up array of first names

public Names1()

{

}

public Names1(String name1)

{

name=name1;

}

publicvoid setName(String suname)

{

this.name=suname;

}

public String getName()

{

return name;

}

publicvoid loadFromFile(String fname)//method loads form text file

{

Scanner in =null;

File infile =new File(fname);

try{

in =new Scanner(infile).useDelimiter("\n");

}

catch (FileNotFoundException fnfe)

{

System.out.println("File not found");

}

int x=1;

while(in.hasNext())

{

name = in.next();

LastName[x] =new Names1(name);

x++;

}

in.close();

}

publicvoid loadFromFile1(String fname)//method loads form text file

{

Scanner in =null;

File infile =new File(fname);

try{

in =new Scanner(infile).useDelimiter("\n");

}

catch (FileNotFoundException fnfe)

{

System.out.println("File not found");

}

int x=1;

while(in.hasNext())

{

name = in.next();

Name[x] =new Names1(name);

x++;

}

in.close();

}

publicvoid test()

{

System.out.println(Name[5].getName()+" "+LastName[5].getName());// this will return robert brown as a test as he is the 5th name.

}

publicvoid test2()

{

System.out.println(Name[5].getName());// this will return robert brown as a test as he is the 5th name.

System.out.println(LastName[5].getName());

}

}

[8142 byte] By [UFC1a] at [2007-11-27 10:35:44]
# 1

Questions:

1) Why do you have last names in a separate file from first names? Wouldn't it be more natural and safer to have them together in a single file, then split the string and extract last and first names?

2) Why keep last and first names in separate arrays? Wouldn't it be more natural and safer to keep them together in an array of name objects?

3) I assume that you want the output to print:

FirstName LastName

and instead you're getting:

FirstName

LastName

How about

System.out.println(Name[5].getName().trim() + " " + LastName[5].getName().trim());

or perhaps trim the input as you are getting it. You are getting line-feed white space at the end of each name. trim() removes this and other white space on the front and backside of the string that calls it.

4) Your arrays are starting from index 1. If you want to do it like everyone else is doing it (might as well start now), you should consider starting the arrays from index 0.

Message was edited by:

petes1234

petes1234a at 2007-7-28 18:36:48 > top of Java-index,Java Essentials,New To Java...
# 2

Yes i'd rather have them in the same class.

Prob being we have to read the names from two different files and we haven't been shown how to do that.

If the names were in the same file i could just do the following

public void loadFromFile(String fname) //method loads form text file

{

Scanner in = null;

File infile = new File(fname);

try {

in = new Scanner(infile).useDelimiter("\t");

}

catch (FileNotFoundException fnfe)

{

System.out.println("File not found");

}

int x=0;

while(in.hasNextLine())

{

surname = in.next();

name = in.next();

person[x] = new names1(surname, name);

x++;

}

in.close();

The want us to say the names into a new file. I.e merge the two files. Format being

robert brown

john smith

etc

UFC1a at 2007-7-28 18:36:48 > top of Java-index,Java Essentials,New To Java...
# 3

have you tried "trim()" as I suggested?

Also, rather than declare arrays of 2001 items, consider looking into using dynamic arrays such as ArrayList. It is much cleaner IMHO.

Message was edited by:

petes1234

petes1234a at 2007-7-28 18:36:48 > top of Java-index,Java Essentials,New To Java...
# 4

Yeah but it prints out

robert

brown

Also in my next method when i search,

public void searchForPlayer()

{

Scanner input = new Scanner(System.in);

System.out.print("\t\tEnter the Players Surname: ");

System.out.flush();

String name = input.nextLine();

int index = -1;

for(int i = 1; i <2000 ; i++)

if(LastName[i].getName().trim().compareTo(name) == 0)

index = i;

if (index >= 0)

{

System.out.println("Player found: "+Name[5].getName()+" "+LastName[index].getName());

}

else

System.out.println("Player not found");

System.out.println("");

}

its returns brown found: robert

man this is wonky.

that being said will be be good enough to let me get on with it and get a pass....

thanks.

UFC1a at 2007-7-28 18:36:48 > top of Java-index,Java Essentials,New To Java...
# 5

You know, if you get rid of the ".useDelimiter("\n");" bit, you wouldn't need to trim a thing. Also, if you know for certainty that both files are exactly the same length, I suppose you could do all of your loading from one method:

// I made a MyName class, that holds both first and last names so that

// I would only need one arraylist that held both.

//This is shown below:

private List<MyName> myNames = new ArrayList<MyName>();

public void loadFromFile(String lastNameFile, String firstNameFile)

{

Scanner scFName = null;

Scanner scLName = null;

try

{

scFName = new Scanner(new File(firstNameFile));// .useDelimiter("\n"); <= don't need this

scLName = new Scanner(new File(lastNameFile));// .useDelimiter("\n");

}

catch (FileNotFoundException fnfe)

{

System.out.println("File not found");

}

if (scFName != null && scLName != null)

{

while (scFName.hasNext() && scLName.hasNext())

{

String fname = scFName.next();// .trim(); if you absolutely needed to trim, you'd do it here

String lname = scLName.next();// .trim();

myNames.add(new MyName(lname, fname)); // here I add to the arraylist

}

scFName.close();

scLName.close();

}

}

petes1234a at 2007-7-28 18:36:48 > top of Java-index,Java Essentials,New To Java...