Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2017
    Posts
    4
    Rep Power
    0

    Default My introduction and question

    I achieved an Associates Degree in Computer Science 20 years ago and did nothing with it (beyond hobbyist status) so my skills are a bit rusty. I am working my way through Head First Java 2nd Edition (the free one) and I'm having an issue I don't understand. I am facing the error message:

    Java Code:
    GameSaverTest.java:30: error: bad operand type String for unary operator '+++'
    			System.out.println("One's type:  ", + oneRestore.getType());
    			                                    ^
    GameSaverTest.java:31: error: bad operand type String for unary operator '+++'
    			System.out.println("Two's type:  ", + twoRestore.getType());
    			                                    ^
    GameSaverTest.java:32: error: bad operand type String for unary operator '+++'
    			System.out.println("Three's type:  ", + threeRestore.getType());
    			                                      ^
    3 errors
    I actually built this example in Eclipse, but went to my terminal because I had an issue copying the error message line to the clipboard. Eclipse said:

    Java Code:
    The operator + is undefined for the argument type(s) String
    Here is the program:

    Java Code:
    import java.io.*;
    public class GameSaverTest implements Serializable {
    	public static void main(String [] args)
    	{
    		GameCharacter one = new GameCharacter(50, "Elf", new String [] {"bow", "sword", "dust"} );
    		GameCharacter two = new GameCharacter(200, "Troll", new String [] {"bare hands", "big axe"});
    		GameCharacter three = new GameCharacter(120, "Magician", new String [] {"spells", "invisibility"});
    		
    		//imagine code that does things with the characters that might change their state values
    		
    		try {
    			ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Game.ser"));
    			os.writeObject(one);
    			os.writeObject(two);
    			os.writeObject(three);
    			os.close();
    		} catch (Exception ex) { ex.printStackTrace(); }
    		
    		one = null;
    		two = null;
    		three = null;
    		
    		try {
    			ObjectInputStream is = new ObjectInputStream (new FileInputStream("Game.ser"));
    			GameCharacter oneRestore = (GameCharacter) is.readObject();
    			GameCharacter twoRestore = (GameCharacter) is.readObject();
    			GameCharacter threeRestore = (GameCharacter) is.readObject();
    
    			
    			System.out.println("One's type:  ", + oneRestore.getType());
    			System.out.println("Two's type:  ", + twoRestore.getType());
    			System.out.println("Three's type:  ", + threeRestore.getType());
    					
    
    		} catch (Exception ex) { ex.printStackTrace(); }
    		
    	}
    
    }
    I Googled the error and the sites that were somewhat relevant to my problem said that one can only + Strings with other Strings. The oneRestore.getType() IS a String. Here's the GameCharacter class:

    Java Code:
    import java.io.*;
    public class GameCharacter implements Serializable {
    	int power;
    	String type;
    	String [] weapons;
    	
    	public GameCharacter(int p, String t, String [] w)
    	{
    		power = p;
    		type  = t;
    		weapons = w;
    	}
    	
    	public int getPower() 
    	{ 
    		return power;
    	}
    	public String getType() 
    	{ 
    		return type;
    	}
    	
    	public String getWeapons() {
    		String weaponList = "";
    		
    		for (int i = 0; i < weapons.length; i++)
    		{
    			weaponList += weapons[i];
    		}
    		return weaponList; 
    	}
    	
    	
    
    }
    So what exactly is happening here, and how do I fix it? They're BOTH Strings!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default Re: My introduction and question

    System.out.println("One's type: ", + oneRestore.getType());
    There should not be a comma when concatenating two Strings with the + operator.

    The comma is often used to separate items in a list.
    For example the printf method in the PrintStream class has a list of args separated by a comma:
    System.out.printf(String format, Object... args);
    Last edited by Norm; 03-18-2017 at 11:11 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Mar 2017
    Posts
    4
    Rep Power
    0

    Default Re: My introduction and question

    Removing the commas fixed it. Thank you. Now, how do I mark this thread as [SOLVED]? I am unable to edit the title line of this thread. Some of the other forums I have accounts on allow me to edit the title of the thread. This one seems not to.

Similar Threads

  1. introduction
    By kyleflores in forum Introductions
    Replies: 0
    Last Post: 11-29-2016, 05:02 AM
  2. Introduction
    By Ryanw327 in forum Introductions
    Replies: 0
    Last Post: 10-20-2010, 07:48 PM
  3. Introduction
    By goldhouse in forum Lucene
    Replies: 2
    Last Post: 01-17-2010, 01:56 PM
  4. Introduction
    By lavalleeb in forum Advanced Java
    Replies: 5
    Last Post: 05-31-2007, 10:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •