LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 685 users online 211448 members 1242 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Memberlist | Dictionary | News | FAQ
Member Spotlight
BleedingSteelWings
Interests: you'll find out.
Mood: Frustrated
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
7 online / 28 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic

Print Statements and Scanners (Java)
Replies: 10Last Post Oct. 31, 2008 8:58pm by Majo
Welcome to LiveWire!
We're Stronger Together.
Join the Community
Single page for this topic Email Print Favorite
( Majo )


Soothsayer
Reply
Code:
import java.util.*;

public class PlayerTest
{
public static void main(String[] args)
{
Player player1 = new Player();
Player player2 = new Player();

Scanner scn = new Scanner(System.in);

System.out.print("Please enter name of the first player: ");
String name = scn.nextLine();
player1.setPlayerName(name);
System.out.println();

int score1;

System.out.print("Please enter scores for player one.\n");

for ( ;;)
{
System.out.print("Please enter score #1: ");
score1 = scn.nextInt();

if(player1.validateScore(score1))
{
break;
}

System.out.println("Invalid score, range 0-100.");
}

int score2;

for( ;; )
{
System.out.print("Please enter score #2: ");
score2 = scn.nextInt();

if(player1.validateScore(score2))
{
break;
}

System.out.println("Invalid score, range 0-100.");
}

int score3;

for( ;; )
{
System.out.print("Please enter score #3: ");
score3 = scn.nextInt();

if(player1.validateScore(score3))
{
break;
}

System.out.println("Invalid score, range 0-100.");
}

System.out.print("Please enter name of the second player: ");
String p2Name = scn.nextLine();
player2.setPlayerName(p2Name);
System.out.println();

int p2Score1;

System.out.print("Please enter scores for player two.");

for( ;; )
{
System.out.print("Please enter score #1: ");
p2Score1 = scn.nextInt();

if(player2.validateScore(p2Score1))
{
break;
}

System.out.println("Invalid score, range 0-100.");
}

int p2Score2;

for( ;; )
{
System.out.print("Please enter score #2: ");
p2Score2 = scn.nextInt();

if(player2.validateScore(p2Score2))
{
break;
}

System.out.println("Invalid score, range 0-100.");
}

int p2Score3;

for( ;; )
{
System.out.print("Please enter score #3: ");
p2Score3 = scn.nextInt();

if(player2.validateScore(p2Score3))
{
break;
}

System.out.println("Invalid score, range 0-100.");
}


System.out.print("\nThe scores for " + player1.getPlayerName() + " are " + score1 + ", " + score2 + ", " + score3 + "; for a total of ");
System.out.print("\nThe scores for " + player2.getPlayerName()  + " are " + p2Score1 + ", " + p2Score2 + ", " + p2Score3 + "; for a total of ");

System.out.print("\nThe highest average score was " + " by ");

System.out.print("\nThe highest game score was");

}
}

I'm supposed to prompt the user to enter the second player's name using a print statement and scanner. I thought I had it set up correctly but it prints out the prompt and then moves on, never allowing me to actually enter a name.

-------
"Under the hardness of her facade a woman's heart is still beating."


7:17 pm on Oct. 29, 2008 | Joined: May 2005 | Days Active: 298
Join to learn more about Majo Pennsylvania, United States | Straight Female | Posts: 6,733 | Points: 11,716
Noraa


we rediscover sensation

Patron
Reply
:heart attack:

-------
all the days were a sun-drenched haze
while the salt spray crusted on the windowpanes...

7:18 pm on Oct. 29, 2008 | Joined: Sep. 2007 | Days Active: 387
Join to learn more about Noraa New York, United States | Label Free Female | Posts: 7,669 | Points: 30,071
( Majo )


Soothsayer
Reply
Quote: from NoNoNora383 at 10:18 pm on Oct. 29, 2008

:heart attack:

Yeah, I know. And that's a small program. They get much bigger.

Oh, and it's not done quite yet.

-------
"Under the hardness of her facade a woman's heart is still beating."


7:20 pm on Oct. 29, 2008 | Joined: May 2005 | Days Active: 298
Join to learn more about Majo Pennsylvania, United States | Straight Female | Posts: 6,733 | Points: 11,716
taraxgoesxboom


Soothsayer

Patron
Reply
It's kinda hard for me to look at this without seeing an assignment sheet. Why are you using player2.setPlayerName(p2Name);
this statement?

Post edited at 7:21 pm on Oct. 29, 2008 by taraxgoesxboom

-------
"I am not immune,
I only want to be loved, but I feel safe behind the firewall"


7:21 pm on Oct. 29, 2008 | Joined: Mar. 2008 | Days Active: 332
Join to learn more about taraxgoesxboom Texas, United States | Female | Posts: 7,718 | Points: 11,868
taraxgoesxboom


Soothsayer

Patron
Reply
System.out.print("Please enter name of the second player: ");  

It might be a syntax error, you forgot the ln on this line.

Are you using J creator? It wont compile it unless there are no syntax errors.

Post edited at 7:27 pm on Oct. 29, 2008 by taraxgoesxboom

-------
"I am not immune,
I only want to be loved, but I feel safe behind the firewall"


7:26 pm on Oct. 29, 2008 | Joined: Mar. 2008 | Days Active: 332
Join to learn more about taraxgoesxboom Texas, United States | Female | Posts: 7,718 | Points: 11,868
( Majo )


Soothsayer
Reply
Quote: from taraxgoesxboom at 10:21 pm on Oct. 29, 2008

It's kinda hard for me to look at this without seeing an assignment sheet. Why are you using player2.setPlayerName(p2Name);  
this statement?

Sorry.

Code:
public class Player
{

private String name;
private int score1;
private int score2;
private int score3;


public Player()
{
name = "No Name";
score1 = 0;
score2 = 0;
score3 = 0;
}


public int getScore1()
{
return score1;
}


public int getScore2()
{
return score2;
}


public int getScore3()
{
return score3;
}


public String getPlayerName()
{
return name;
}


public void setPlayerName(String _name)
{
name = _name;
}

public void setScore1(int _score1)
{
score1 = _score1;
}


public void setScore2(int _score2)
{
score2 = _score2;
}


public void setScore3(int _score3)
{
score3 = _score3;
}


public boolean validateScore(int score)
{
if((score >= 0) && (score <= 100))
{
return true;
}
return false;
}


public int calcTotal()
{
return score1+score2+score3;
}


public double calcAverage()
{
int total = calcTotal();
return (double)total/3.0;
}


public int calcHighScore()
{
int high = score1;
if(score2 > high)
{
high = score2;
}
if(score3 > high)
{
high = score3;
}
return high;
}

}



-------
"Under the hardness of her facade a woman's heart is still beating."


7:27 pm on Oct. 29, 2008 | Joined: May 2005 | Days Active: 298
Join to learn more about Majo Pennsylvania, United States | Straight Female | Posts: 6,733 | Points: 11,716
( Majo )


Soothsayer
Reply
Quote: from taraxgoesxboom at 10:26 pm on Oct. 29, 2008

System.out.print("Please enter name of the second player: ");

It might be a syntax error, you forgot the ln on this line.

Are you using J creator? It wont compile it unless there are no syntax errors.


No, I'm using jGRASP.

I think I've tried println but I'll try it again.

-------
"Under the hardness of her facade a woman's heart is still beating."


7:28 pm on Oct. 29, 2008 | Joined: May 2005 | Days Active: 298
Join to learn more about Majo Pennsylvania, United States | Straight Female | Posts: 6,733 | Points: 11,716
taraxgoesxboom


Soothsayer

Patron
Reply
Quote: from Majo at 7:28 pm on Oct. 29, 2008

Quote: from taraxgoesxboom at 10:26 pm on Oct. 29, 2008

System.out.print("Please enter name of the second player: ");    

 It might be a syntax error, you forgot the ln on this line.  

 Are you using J creator? It wont compile it unless there are no syntax errors.


No, I'm using jGRASP.

I think I've tried println but I'll try it again.


Sorry, other than that I don't really see what it's missing. Right now I'm working on arrays and their still kicking my butt, lol. Good luck.

-------
"I am not immune,
I only want to be loved, but I feel safe behind the firewall"


7:33 pm on Oct. 29, 2008 | Joined: Mar. 2008 | Days Active: 332
Join to learn more about taraxgoesxboom Texas, United States | Female | Posts: 7,718 | Points: 11,868
( Majo )


Soothsayer
Reply
println didn't work. I don't know what's wrong.

We're working on arrays, too. They're kicking my ass as well.

-------
"Under the hardness of her facade a woman's heart is still beating."


7:36 pm on Oct. 29, 2008 | Joined: May 2005 | Days Active: 298
Join to learn more about Majo Pennsylvania, United States | Straight Female | Posts: 6,733 | Points: 11,716
matto


Enlightened One

Patron
Tech Support Leader
Reply
Can you post it with indents?  Hella hard to troubleshoot when it's like that.

Also, if I were writing such a program, I'd implement a method called getInfo() or something within Player.java that input a Player object and prompted the user for and properly stored all of the information.  It'd shorten the amount of code significantly, and make troubleshooting much easier (if required at all).  Then your main would look something like

Player player1 = new Player();
Player player2 = new Player();
player1.getInfo();
player2.getInfo();
System.out.println(player1);
System.out.println(player2);

If you put a toString() method in Player.java, then you can do the above.  System.out.println(player1) will print out the value of player1.toString() if such a method exists, also reducing the redundancy of your code.

My bad if this is hella useless information.  I dunno what's wrong with your code because reading it is a bitch and it's difficult to find problems with it in its current, unindented form.  So yeah.

You in AP CS?  It's teh shit I'm in that class right now ...

-------
"Strong am I with the Force, but not that strong.
Twilight is upon me, and soon, night must fall.
That is the way of things. The way of the Force."
--Yoda


12:54 am on Oct. 30, 2008 | Joined: Aug. 2007 | Days Active: 437
Join to learn more about matto California, United States | Male | Posts: 8,818 | Points: 16,846
( Majo )


Soothsayer
Reply
I'm in Programming for Info Tech I.

Sorry about the indents thing, I tried but every time I hit tab, my cursor jumped from within the text box to some other point on the screen so I gave up.

As it turns out, taking out Line in scn.nextLine() did it. I have no idea why though.

-------
"Under the hardness of her facade a woman's heart is still beating."


8:58 pm on Oct. 31, 2008 | Joined: May 2005 | Days Active: 298
Join to learn more about Majo Pennsylvania, United States | Straight Female | Posts: 6,733 | Points: 11,716
Single page for this topic Email Print Favorite

Quick Reply

You are signed in as our guest.

Looking for something else?
 

  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic