#I need help

196 messages Β· Page 1 of 1 (latest)

random agate
#

i don’t understand ;-; why me 😦

balmy dockBOT
#

βŒ› This post has been reserved for your question.

Hey @random agate! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

untold apex
#

You don't need to recreate the scanner like that

#

For one

#

Looks like that's what the problem is

#

The compiler is complaining about you redefining the Scanner every time you need it (Looks like a case of accidentally copy-pasting it 😝)

#

Just create it once at the beginning of your class

#

It stays in memory until the main method stops executing

final crane
#

Yeah, you only need to declare one scanner object then you can reuse it

random agate
#

yes

#

i see

#

but now

#

it has another issue

final crane
#

Show

random agate
untold apex
final crane
#

You are doing next int

random agate
#

Im a 14 year old in highschool ;-;

final crane
#

You need next line

random agate
#

yes

#

uhhh

#

confused

untold apex
#

btw, you shouldn't name variables like that

#

keep them lowercase

random agate
#

i see

#

its a basic csc 151 class i dont think she would mind

#

how do i do the next line function?

#

is it like

#

inputDevice.nextLine();

#

?

final crane
#

Yes

random agate
#

;-;

untold apex
#

That's just a command issue I think

#

I don't think you broke anything

random agate
#

i dont get it

untold apex
#

I'm used to using an IDE πŸ™ƒ

random agate
#

why am i such a bad coder

untold apex
#

Hey, don't worry.

#

Try using javac again

final crane
#

I dont think its your fault it really just looks like whatever shitty online IDE you are using fault

#

Switch to repl.it if you want a online IDE

untold apex
#

Unless your teacher said you had to use this garbage cengage thing

random agate
#

no no

#

this is on cengage

#

its an assignment

#

my professor assigned this to me

#

this is a college course so i need to finish this properly ;-;

final crane
#

change your class name to Main

random agate
#

I can't

final crane
#

why

random agate
#

not allowed

final crane
#

but its throwing an error because of it

random agate
#

I changed to string

#

like it asked me to

#

now my math commands are wrong ig

final crane
#

do ls

random agate
#

huh

final crane
#

actually

#

can u paste all ur code here for a second

random agate
#

sure

#

import java.util.Scanner;
public class ElectionStatistics{
public static void main(String[] args){
String Party1;
String Party2;
String Party3;
double Percentage1;
double Percentage2;
double Percentage3;
String Votes1;
String Votes2;
String Votes3;
int TotalVotes = 165;
int Hundred = 100;

    System.out.println("Enter name for first party >>");
    Scanner inputDevice = new Scanner (System.in);
    Party1 = inputDevice.nextLine();
    System.out.println("Enter votes received >>");
    Votes1 = inputDevice.nextLine();

    System.out.println("Enter name for second party >>");
    Party2 = inputDevice.nextLine();
    System.out.println("Enter votes received >>");
    Votes2 = inputDevice.nextLine();
    
    System.out.println("Enter name for third party >>");

    Party3 = inputDevice.nextLine();
    System.out.println("Enter votes received >>");
    Votes3 = inputDevice.nextLine();

    Percentage1 = Votes1/TotalVotes*Hundred;
    Percentage2 = Votes2/TotalVotes*Hundred;
    Percentage3 = Votes3/TotalVotes*Hundred;
    System.out.println("The" + Party1 + "got" + Percentage1 + "percent of the vote");
    System.out.println("The" + Party2 + "got" + Percentage2 + "percent of the vote");
    System.out.println("The" + Party3 + "got" + Percentage3 + "percent of the vote");
    



    
    
}

}

final crane
random agate
#

yes

#

how to i do it then

untold apex
#

Strings are immutable

random agate
#

i need to convert it into a value first?

final crane
#

well not only that but it also makes no sense to divide a string lol

random agate
#

i input a value for votes 1

#

lets say 25

#

then it divides by the total number

#

lets say 189

untold apex
#

use .nextInt()?

random agate
#

and multiplies by 100

final crane
random agate
#

to get percent

final crane
#

you can just change its datatype to a int and do .nextInt

random agate
#

how

#

can u show

final crane
#

ok

#

/run java

import java.util.Scanner;

public class Example {

     public static void main(String[] args) {
          final Scanner sc = new Scanner(System.in);
          int foo;
          foo = sc.nextInt();

          System.out.println(foo);
      }

}```
5
little yokeBOT
#

Here is your java(15.0.2) output @final crane

5
random agate
#

but

#

they are strings

#

they need to be

#

for me to use

#

nextLine

final crane
#

why do you have to use nextLine

random agate
#

IDK you told me to...

final crane
#

yeah well you were trying to store and int in a string thats why

#

nextLine returns a string

#

nextInt returns a int

random agate
#

why do i need a string return

final crane
#

you dont

#

thats whats causing the error

#

you can just change the string to a int and do nextInt

#

then you will be able to divide it

#

you cant divide strings

random agate
#

were back here now

final crane
#

You are making me go insane dude

random agate
#

IM GOING INSANE

#

this is due at 11:55

final crane
#

Change these datatypes to string and change the .nextInt to .nextLine

random agate
#

i understand why those are strings...

#

their words

#

im kinda slow

#

but now

#

this is happening

#

it wont let me

#

do the second party

#

before taking me to second votes

final crane
#

After these 2 nextInt do inputDevice.nextLine()

random agate
#

alr bet it works now

#

but

#

the math doestn workl

#

πŸ™‚

untold apex
#

harold moment

random agate
untold apex
#

Just a hunch

#

try typecasting everything to doubles

#

Hold on lemme type it out

#
(double) Votes1 / (double) TotalVotes * Hundred;
final crane
#

(Votes1/TotalVotesHundred)*Hundred

#

this this if that not work

untold apex
#

Oh I misread

#

Honestly when I do any math involving doubles I just make sure all the variables are doubles so I don't have to worry about typecasting

random agate
#

you were absolutely right

#

making them doubles worked

untold parcel
#

you only really need one cast, usually (double) Votes1 / TotalVotes * Hundred is used
although you could just have Hundred be a double and do Votes1 * Hundred / TotalVotes, or just not use a variable for that, Votes1 * 100.0 / TotalVotes or Votes1 * 100d / TotalVotes

random agate
#

did i do anything that makes it seem like i copied code

#

anything too advanced

untold parcel
#

you generally don't want to be using doubles for values that are actually integers

random agate
#

im week 3 of java basics im dying rn

untold parcel
#

(not the Hundred, since that's just conceptual, and it kinda is unnecessary as a whole tbh...)

untold apex
#

I don't think you did anything too advanced

random agate
#

alr bet

#

ez 100

untold parcel
#

also btw, you should use camelCase for variables

#

not PascalCase

untold apex
#

This ^^

#

PascalCase for classes though

untold parcel
#

for types*

#

classes, interfaces, enums, records, annotations

untold apex
#

Objects too

#

ye

untold parcel
#

objects don't get names

untold apex
#

object references?

#

Oh wait

#

I'm thinking of classes

untold parcel
#

object references are just integers lol

#

for completeness;
java (and most other c-style languages iirc):
camelCase for variables, methods, members, functions
PascalCase for types (classes, interfaces, etc)
MACRO_CASE for conceptual constants (not equivalent to final!)

random agate
#

this discord server is hurting my braincells

#

LETS GO

#

so

untold parcel
#

i feel like there's a metaphor in there somewhere about improving...

random agate
#

the code

#

didnt work perfectly

#

becuase i realized

#

the total amount of votes isnt always going to be the same

#

so after Doubles Votes1/2/3 were used

#

I added

#

TotalVotes = Votes1/2/3 summed

#

yk

#

i troubleshooted my code

#

for the first time

#

EZ

untold apex
random agate
#

ik

#

being a highschool freshman

#

were not allowed to take college classes this advanced

#

but im special

#

so yk

#

they let me

#

which is cool and im excited so its cool to learn something

untold apex
#

Don't mean to scare you, but just wait until you get into Objects πŸ™ƒ

random agate
#

no

#

no

#

no

#

im going to drop out

#

haha

untold apex
#

no don't, programming is fun

random agate
#

I want to be a software engineer

untold apex
#

Just when you pace yourself and don't let yourself get overwhelmed

#

Same

random agate
#

graduate from mit or carnegie mellon

#

hopefully

balmy dockBOT
untold parcel
#

seems like thisd be helpful to you

untold apex
#

Have this emoji of my Java professor

random agate
#

Thank you guys bye! see yall around when I need help on this weeks assignments πŸ™‚