Jump to content



Photo

[Java]Linked List Implementation


  • Please log in to reply
18 replies to this topic

#16 OP SpeedyTheSnail

SpeedyTheSnail

    Resident Snail Herder

  • Joined: 29-June 04
  • Location: Caprica

Posted 06 May 2014 - 01:13

Well now I have a linked list that works, sort of. My problem is when I am taking an integer input, it somehow has a different value than what I put in. For example:

Enter your house number:

Input: 519

Output: 551

 

What could cause number to add to it, when there are no other methods that modify the variable houseNumber? And I hope you don't ask for my code, as it looks like a three year old could write with more organization!

		System.out.println("Enter your house number:");
		int houseNum = in1.nextInt();
		System.out.println("Enter your street name:?");
		String street = in1.nextLine();
		System.out.println("Enter your city:");
		String city = in1.nextLine();
		System.out.println("Enter your state abrreviation:");
		String state = in1.nextLine();
		System.out.println("Enter your zip code:");
		int zip = in1.nextInt();


.........
			while(null != (iter = addressBook.getNext(iter))){
				System.out.println(iter.HouseNum()+' '+iter.Street());
				System.out.println(iter.City()+ ", "+iter.State()+' '+iter.ZipCode());		
			}



#17 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 11
  • Joined: 26-October 05

Posted 06 May 2014 - 01:49

 

Well now I have a linked list that works, sort of. My problem is when I am taking an integer input, it somehow has a different value than what I put in. For example:

Enter your house number:

Input: 519

Output: 551

 

What could cause number to add to it, when there are no other methods that modify the variable houseNumber? And I hope you don't ask for my code, as it looks like a three year old could write with more organization!

I suspect this has to do with:

 iter.HouseNum()+' '

This is basically equivalent to:

iter.HouseNum() + (int)' '

Which is

iter.HouseNum() + 32

Which, as you may guess, is just another integer, and not the string concatenation you thought you were doing.

 

Just use " " instead of ' '.



#18 OP SpeedyTheSnail

SpeedyTheSnail

    Resident Snail Herder

  • Joined: 29-June 04
  • Location: Caprica

Posted 06 May 2014 - 01:52

I suspect this has to do with:

 iter.HouseNum()+' '

The sum of an integer and a character is the sum of the integer and the integral value of the character according to the ASCII table.

 

Just use " " instead of ' '.

Thank you!!



#19 OP SpeedyTheSnail

SpeedyTheSnail

    Resident Snail Herder

  • Joined: 29-June 04
  • Location: Caprica

Posted 06 May 2014 - 04:14

I finally finished the assignment, thank you for all the help! I really appreciate it, I've been behind for a while with the death in my family as well as my new baby I expect this week! I'd buy you all beers over the internet if I could.