Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

807600Jul 14 2007 — edited Jul 14 2007
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;

public class NameFinder
{
	public static void 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;

public class 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;
	}

	public void setName(String suname)
	{
		this.name=suname;
	}

	public String getName()
	{
		return name;
	}

	public void 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();

	    }

	 public void 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();

	    }

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

public void 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());
	}



}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 11 2007
Added on Jul 14 2007
5 comments
162 views