As you can see below elements [1] and elements[2] have both been converted to integers, I was wondering if it is possible to add then together each time the while loop goes around. For example elements 1 is the score of each game so I was wondering if it is possible to add each game score together to get the total score of all the games which have been entered by the user.
import java.util.Scanner;
public class REQ1
{
public static void main (String[] args)
{
String playername;
String line;
String[] list = new String[100];
int count = 0;
int score;
int time;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your name");
playername = sc.nextLine();
System.out.println("Please enter your game achivements (Game name:score:time played) E.g. Minecraft:14:2332");
while (count < 100){
line = sc.nextLine();
if(line.equals("quit")){
break;
}
list[count]=line;
System.out.println("list[count]" + list[count]);
count++;
}
System.out.println("Player : "+playername);
System.out.println("--------------------------------");
for (int i=0; i<count; i++){
line=list[i];
String[] elements =line.split(":");
score=Integer.parseInt(elements[1].trim());
time=Integer.parseInt(elements[2].trim());
System.out.println("Game:" +elements[0]+ " Score= "+elements[1]+" Minutes Played= "+elements[2]);
}
}
}
Yeah it is absolutely possible.
You can just do something like this
int totalScore = 0; //INITIAlISE THE VAR TOTAL SCORE
for (int i=0; i<count; i++){
line=list[i];
String[] elements =line.split(":");
score=Integer.parseInt(elements[1].trim());
time=Integer.parseInt(elements[2].trim());
totalScore = totalScore + score;// ADD THIS LINE
System.out.println("Game:" +elements[0]+ " Score= "+elements[1]+" Minutes Played= "+elements[2]);
}
score+=Integer.parseInt(elements[1].trim());
time+=Integer.parseInt(elements[2].trim());
Related
import java.util.Scanner;
public class Question1 {
public static void main(String[] args){
System.out.println(" Welcome to Shopping");
System.out.println("Please enter your shopping (item$price and seperate by comma): ");
Scanner scanner = new Scanner(System.in);
String list = scanner.next();
//here is my code
System.out.println("Here is the list of items:");
int p = 0;
int count = 0;
for(int i = 0; i < list.length(); i++) {
if(list.charAt(i) == ',')
count++;
}
int[] price = new int[count];
String[] temp = list.split("\\,");
for(int i=0;i<=count;i++){
int a = i+1;
**price[i] = Integer.parseInt(temp[i].substring(temp[i].indexOf("$")+1,temp[i].length()));**
p+=price[i];
System.out.println("No."+a+" "+temp[i].substring(0,temp[i].indexOf("$")));
}
System.out.println("The total price is: $"+p+".");
System.out.println("Thank you for using this program!!");
}
}
}
//I dont know why it couldnt print out,and shows something like arrary
out of ...
I try to print out something like Type nameoftheitem$price,nextone.It
will looks like
mango$12,
tomato$14,
print out like
1.mango
2.tomato
at the end,print out its sum cost 26
It works with changing the i to 1 for the first for loop ,and it works well accompanying with setting i<count
i wrote a code using array and method that allow the user to enter any number of numbers and
display the numbers sorted from the smallest number to the largest number however the program works but it doesn't show the numbers here is the code that i wrote
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("How many numbers you want to enter? ");
int size = s.nextInt();
int i;
double[] numbers1 = new double[size];
System.out.println("Enter " + numbers1.length + " numbers: ");
getNumbers(numbers1);
double[] numbers2 = new double[numbers1.length];
for (i = 0; i < numbers1.length; i++) {
numbers2[i] = numbers1[i];
}
displayNumbers(numbers1);
System.out.println("The numbers after sorting are: ");
sortNumbers(numbers2);
displayNumbers(numbers2);
}
public static void getNumbers(double[] numbers) {
Scanner s = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = s.nextDouble();
}
}
public static void sortNumbers(double[] numbers) {
double temp;
double pass;
for (pass = 0; pass < numbers.length; pass++) {
for (int i = 0; i < numbers.length - 1; i++) {
if (numbers[i] > numbers[i + 1]) {
temp = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = temp;
}
}
}
}
public static void displayNumbers(double[] numbers) {
Scanner s = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = s.nextDouble();
System.out.print(numbers + " ");
}
System.out.println();
}
}
Your displayNumbers() method is wrong. In the loop you wrote:
numbers[i] = s.nextDouble();
System.out.print(numbers + " " );
You're trying to read again 4 doubles (everytime you call that method) and you're printing the whole array (which doesn't do what you'd expect). What you probably want is this:
System.out.print(numbers[i] + " " );
Your code is inefficient and unclear.
You don't need to create a new Scanner everytime and also why instead of println the array with Arrays.toString(double[]).
You should also make more cleaner instructions and variable names.
Here is an example of how the code should be
public static void main(String[] args)
{
//create a new Scanner with a name that defines it
Scanner scanner = new Scanner(System.in);
//print your instructions
System.out.println("How many numbers would you like to sort?");
//print "> " to let the user to know he should be entering values
System.out.print("> ");
//read the number the user has entered which we will define as how many numbers he would enter next
int totalNumbers = scanner.nextInt();
//create a new array the size of the total numbers
double[] unsortedNumbers = new double[totalNumbers];
//tell the user to enter his unsorted numbers
System.out.println("Enter your unsorted numbers: ");
//loop totalNumbers times until the whole unsortedNumbers is full
for(int index = 0; index < totalNumbers; index++)
{
System.out.print("> ");
unsortedNumbers[index] = scanner.nextDouble();
}
//Print the numbers he entered
System.out.println("You entered: ");
//Arrays.toString prints the array in format [number, number, ...]
System.out.println(Arrays.toString(unsortedNumbers));
//sort the arrays with Arrays.sort which sorts in ascending numerical order
Arrays.sort(unsortedNumbers);
//Print the final result - sorted numbers
System.out.println("Sorted: " + Arrays.toString(unsortedNumbers));
}
How can I use a for loop asking for student[0]'s grade, [1]'s etc? As of now, my for loop is pointless.
public static void main(String[] args) {
//declare grade and student array
int[] grades = new int[5];
String[] students = { "Bill", "Tom", "Mark", "Nic", "Miguel"};
Scanner sc = new Scanner(System.in);
for(int i = 0; i < grades.length; i++) {
grades[i] = i + 1;
//user input of student's grade
System.out.print("Enter grade for " + students[0] + ": ");
grades[0] = sc.nextInt();
System.out.print("Enter grade for " + students[1] + ": ");
grades[1] = sc.nextInt();
}
}
There must be a better way than hardcoding each student and grade array.
You can take the input and assign it in the loop itself.
for(int i = 0; i < grades.length; i++)
{
System.out.print("Enter grade for " + students[i] + ": ");
grades[i] = sc.nextInt();
}
That's all. You do not need to repeat code as that is what the loop will do by accessing each element with the index i.
However, I am not sure what the line grades[i] = i + 1; accomplishes in your code, since you anyways overwrite that value with the one you get as input.
If i am interpreting your question correct, what you want to do is accept grade values from user using for loop? My answer is as below, I have also printed grades which are accepted.
public static void main(String[] args) {
//declare grade and student array
int[] grades = new int[5];
String[] students = { "Bill", "Tom", "Mark", "Nic", "Miguel"};
Scanner sc = new Scanner(System.in);
System.out.println("please enter the grades of students");
for(int i=0; i<students.length; i++){
grades[i]= sc.nextInt();
}
for(int i=0; i<grades.length; i++){
System.out.println(grades[i]);
}
}
The code below shows my progress, but I cannot print the numbers that were entered. I don't know where to put println("you entered the following numbers") in the loop so that it'll show up when the loop stops.
import java.util.Scanner;
public class aufgabe5 {
public static void main(String[] args) {
int x;
Scanner input = new Scanner(System.in);
System.out.println("How much numbers do you want to enter?");
x = input.nextInt();
int j = 1;
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[x];
for (int i = 0; i < numbers.length; i++) {
System.out.println("Enter the " + j++ + ". number:");
numbers[i] = scanner.nextInt();
}
System.out.println("You entered following numbers");
System.out.println(x);
}
}
Change x like
System.out.println(x);
to Arrays.toString(int[]) like
System.out.println(Arrays.toString(numbers));
Edit
To print the array reversed,
String str = Arrays.toString(numbers).replace(", ", " ,");
str = str.substring(1, str.length() - 1);
System.out.println(new StringBuilder(str).reverse().insert(0, "[")
.append("]"));
Intialize the String before the loop, then keep adding the values to it.
After the loop finishes you can print the whole thing.
String someVar="You entered following numbers: ";
for(int 1=0;i<array.length;i++)
{
someVar=someVar+numbers[i]+",";
}
System.out.println(someVar);
I have a loop that is supposed to store information into an array of objects, but for some reason, it always skips the first input.
public class GerbilData {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("How many different food items do the gerbils eat?");
int n1 = keyboard.nextInt();
Food[] gerbilFood = new Food[n1];
String temp;
int temp2;
int count = 1;
for (int a = 0; a < n1; a++){
gerbilFood[a] = new Food();
}
int j = 0;
while (j < n1){
System.out.println("Name of food item " + count + ":");
temp = keyboard.nextLine();
gerbilFood[j].setName(temp);
count++;
j++;
}
keyboard.nextInt() is only reading an integer from the keyboard, not reading the return character. So, when you first call keyboard.nextLine() you get the \n of the getInt().
Try this instead :
int n1 = keyboard.nextInt();
keyboard.nextLine();