#I need help
196 messages Β· Page 1 of 1 (latest)
β This post has been reserved for your question.
Hey @random agate! Please use
/closeor theClose Postbutton 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.
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
Yeah, you only need to declare one scanner object then you can reuse it
Show
Ah, tis is the life of a software developer
You are doing next int
Im a 14 year old in highschool ;-;
You need next line
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();
?
Yes
i dont get it
I'm used to using an IDE π
why am i such a bad coder
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
Unless your teacher said you had to use this garbage cengage thing
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 ;-;
change your class name to Main
I can't
why
not allowed
but its throwing an error because of it
do ls
huh
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");
}
}
its because you are trying to divide a string
Strings are immutable
i need to convert it into a value first?
well not only that but it also makes no sense to divide a string lol
i input a value for votes 1
lets say 25
then it divides by the total number
lets say 189
use .nextInt()?
and multiplies by 100
no
to get percent
you can just change its datatype to a int and do .nextInt
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
Here is your java(15.0.2) output @final crane
5
why do you have to use nextLine
IDK you told me to...
yeah well you were trying to store and int in a string thats why
nextLine returns a string
nextInt returns a int
why do i need a string return
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
were back here now
You are making me go insane dude
Change these datatypes to string and change the .nextInt to .nextLine
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
After these 2 nextInt do inputDevice.nextLine()
moment
Just a hunch
try typecasting everything to doubles
Hold on lemme type it out
(double) Votes1 / (double) TotalVotes * Hundred;
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
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
you generally don't want to be using doubles for values that are actually integers
im week 3 of java basics im dying rn
(not the Hundred, since that's just conceptual, and it kinda is unnecessary as a whole tbh...)
I don't think you did anything too advanced
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!)
i feel like there's a metaphor in there somewhere about improving...
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
Sorry lol, I just got excited because I know way more terminology now than I did when I joined this server so now it feels like I actually know what I'm talking about π
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
Don't mean to scare you, but just wait until you get into Objects π
no don't, programming is fun
I want to be a software engineer
seems like thisd be helpful to you
Thank you guys bye! see yall around when I need help on this weeks assignments π
