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
Related
how do I make two arraylist with the same size
like when I stop at index 5 in the first arraylist the second array list will automatically stop when i reach the index 5
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList <String> title = new ArrayList<>();
ArrayList <String> description = new ArrayList<>();
int i = 0;
int d = 0;
String n = in.nextLine();
while(!n.equals(" ")){
System.out.println("Enter a movie title");
title.add(n);
n = in.nextLine();
}
for(;i < title.size(); i++){
System.out.println("[" + i+"]" +title.get(i));
}
description = new ArrayList<>(title.size());
String m = in.nextLine();
while(!m.equals(" ")){
System.out.println("Enter the movie Description");
description.add(m);
m = in.nextLine();
}
description.remove(0);
for(;d < description.size(); d++){
System.out.println("Description for ["+d+"]"+title.get(d)+":"+description.get(d) );
}
}
}
Add a break statement.
// I'm assuming here is where you want it.
// description = new ArrayList<>(title.size());
String m = in.nextLine();
while(!m.equals(" ") && description.size() < title.size()){
System.out.println("Enter the movie Description");
description.add(m);
m = in.nextLine();
}
If you really want the same amount, then don't allow them to exit.
String m = in.nextLine();
while(description.size() < title.size()){
System.out.println("Enter the movie Description");
description.add(m);
m = in.nextLine();
}
for(;d < description.size() && d < title.size(); d++)
This still does not ensure that there are equally many titles and descriptions.
Perhaps you also want to move int i = 0; and int d = 0; into the head of their corresponding for-loops (like for(int i = 0; i < title.size(); i++)). And you may want to change your while-loops to do-while-loops to avoid inserting (and even waiting/"asking" for) the first input line, which will be read before your prompt is written.
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());
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);
Like c++ we can use this.
a[5];
copy(istream_iterator<int>(cin),istream_iterator<int>(),a);
Is there some simple way to get array from input in Java?
You can use the simple way like it
package userinput;
import java.util.Scanner;
public class USERINPUT {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//allow user input;
System.out.println("How many numbers do you want to enter?");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the " + num + " numbers now.");
for (int i = 0 ; i < array.length; i++ ) {
array[i] = input.nextInt();
}
//you notice that now the elements have been stored in the array .. array[]
System.out.println("These are the numbers you have entered.");
printArray(array);
}
//print the array
public static void printArray(int arr[]){
int n = arr.length;
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
How about simply using System.console().readLine("Array (use , as separator)? ").split(",")? Then convert it to an int[] array (there are tons of util libraries to do that).
I was wondering how to load up an array (with user input) using a while loop. The code below prints a 0.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = 0;
int n = 0;
int[] myArray = new int[10];
System.out.printf("enter a value>>");
while (scan.nextInt() > 0) {
for (i = 0; i > 0; i++) {
myArray[i] = scan.nextInt();
}
System.out.printf("enter a value>>");
}
System.out.printf("array index 2 is %d", myArray[2]);
}
There are multiple things wrong with your code:
First of all
while(scan.nextInt() > 0){
Scanner.nextInt() returns an int from your standard input so you actually have to pick up that value. You are checking here what the user typed but then not using it at all and storing the next thing that the user types by saying:
myArray[i] = scan.nextInt();
You don't really need the outer while loop, just use the for loop, its enough.
However, your for loop is off as well:
for(i = 0; i > 0; i++){
It starts at i equal to 0 and runs while i is greater than 0. This means it will never actually run the code within the loop because 0 is never greater than 0. And if it did run (you started it at some number < 0), you would end up in an infinite loop because your condition i > 0 is always true for positive numbers.
Change the loop to:
for(i = 0; i < 10; i++){
Now, your loop could look like:
for(i = 0; i < 10; i++){ // do this 10 times
System.out.printf("enter a value>>"); // print a statement to the screen
myArray[i] = scan.nextInt(); // read an integer from the user and store it into the array
}
one other way to do it
Scanner scan = new Scanner(System.in);
List list = new ArrayList();
while(true){
System.out.println("Enter a value to store in list");
list.add(scan.nextInt());
System.out.println("Enter more value y to continue or enter n to exit");
Scanner s = new Scanner(System.in);
String ans = s.nextLine();
if(ans.equals("n"))
break;
}
System.out.println(list);
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int[] arr=new int[4];
int i;
for(i=0;i<4;i++)
{
System.out.println("Enter the number: ");
arr[i]=input.nextInt();
}
for(i=0;i<4;i++)
{
System.out.println(arr[i]);
}
}
Hope this code helps.